【发布时间】:2016-03-26 00:41:47
【问题描述】:
我正在学习统一,我正在尝试从 XNA 重新创建我在 Unity 中的游戏。
我在 youtube 上关注来自 unity 的 Tutorial Playlist,我使用 GameManager 和 BoardManager 来创建我的地图。
这是我在墙上预制件上的检查员
这是我的 Player 预制件上的检查器
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 左为 false),则会发生碰撞,但盒子会移动,这与我的意图相反。当我检查Is Kinetic时,没有再次发生碰撞。
【问题讨论】:
-
什么版本的统一(精确)?
-
好吧,不是那个问题。其次,物理有什么不寻常的地方,你能贴出墙上的脚本吗?
-
哦,忘了说 - 墙上的脚本只是空的,我可以把它完全删除。这是教程墙脚本的其余部分,我最终没有实施,因为它不适合我的游戏。
-
你改变了Physics2D,有不同的碰撞层吗?
-
按照教程,我创建了一个名为 BlockingLayer 的新层(以及一个新的排序层,但不是这样),播放器和墙预制件都分层为 BlockingLayer。除此之外,我没有与 Physics2D 互动。