我正在学习统一性,并且正在尝试从XNA在Unity中重新创建自己的游戏。
我正在youtube上按照本教程播放列表进行操作,并且已经使用GameManager和BoardManager来创建地图。
这是我的墙壁预制件上的检查员这是我的播放器预制件上的检查员
PlayerMovement
脚本代码
using UnityEngine;
namespace Assets.Scripts
{
public enum Directions
{
Back,
Left,
Front,
Right,
Idle = -1
}
public class PlayerMovement : MonoBehaviour
{
#region Public Members
public float speed;
#endregion
#region Constants
private const float DECAY_FACTOR = 0.85f;
private const float SPEED_FACTOR = 20000f;
#endregion
#region Private Members
private Rigidbody2D rb2D;
private Vector2 velocity;
private Animator animator;
#endregion
#region Game Loop Methods
private void Awake()
{
animator = GetComponent<Animator>();
rb2D = GetComponent<Rigidbody2D>();
}
private void Update()
{
float vertical = Input.GetAxisRaw("Vertical");
float horizontal = Input.GetAxisRaw("Horizontal");
UpdateVelocity(vertical, horizontal);
UpdateAnimation();
UpdateMovment();
}
#endregion
#region Animation Methods
private void UpdateAnimation()
{
Directions direction;
if (velocity.y > 0)
direction = Directions.Back;
else if (velocity.y < 0)
direction = Directions.Front;
else if (velocity.x > 0)
direction = Directions.Right;
else if (velocity.x < 0)
direction = Directions.Left;
else
direction = Directions.Idle;
SetDirection(direction);
}
private void SetDirection(Directions value)
{
animator.SetInteger("Direction", (int)value);
}
#endregion
#region Movement Methods
private void UpdateMovment()
{
Debug.Log(string.Format("HOR - {0} : VER - {1} : DIR - {2}", velocity.x, velocity.y, animator.GetInteger("Direction")));
transform.Translate(velocity.x, velocity.y, 0f, transform);
ApplySpeedDecay();
}
private void UpdateVelocity(float vertical, float horizontal)
{
if (vertical != 0)
velocity.y += Mathf.Abs(speed) / SPEED_FACTOR;
if (horizontal != 0)
velocity.x += Mathf.Abs(speed) / SPEED_FACTOR;
}
private void ApplySpeedDecay()
{
// Apply speed decay
velocity.x *= DECAY_FACTOR;
velocity.y *= DECAY_FACTOR;
// Zerofy tiny velocities
const float EPSILON = 0.01f;
if (Mathf.Abs(velocity.x) < EPSILON)
velocity.x = 0;
if (Mathf.Abs(velocity.y) < EPSILON)
velocity.y = 0;
}
#endregion
}
}
这是我在游戏中遇到的问题的一个示例:
如您所见,玩家可以简单地进出墙壁,就像他们没有盒子对撞机一样。
在写这篇文章时,我注意到,如果我给墙壁预制件提供Rigidbody2D(Is Kinetic
左为假),则会发生碰撞,但盒子会移动,这与我的意图相反。当我检查时Is Kinetic
,不再有冲突。
编辑-您的播放器已选中“ iskinematic”!我相信这是您的问题!
通过统一的文档“如果启用isKinematic,部队,碰撞或接缝不会影响刚体了。 ” - http://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html
1.)确保“墙”具有Rigidbody2D和Rigidbody2D.isKinematic已选中
2.)检查您的碰撞矩阵。我注意到“玩家”具有一层“ BlockingLayer”。(您可能想要更改它),但是如果在“编辑”->“项目设置”->“ Physics2D”中未选中“ BlockingLayer” x“ BlockingLayer”复选框,则可以解释缺少冲突。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句