我在Unity中遇到了这个奇怪的碰撞错误,这是它的GIF演示:
重现方法:在此GIF中,例如,我同时按下左箭头和上箭头,直到速度归一化,然后我会被困在一个块中。
在我用XNA开发这个游戏时,我曾经有过自己的碰撞算法出现过这种问题,但我希望在Unity中不会出现。
这是玩家脚本PlayerMovement
:
using UnityEngine;
using UnityEngine.UI;
namespace Assets.Scripts
{
public enum Directions
{
Back,
Left,
Front,
Right,
Idle = -1
}
public class PlayerMovement : MonoBehaviour
{
#region Public Members
/// <summary>
/// Maximum speed of the player (Accelerated to over a period of time)
/// </summary>
public float speed;
/// <summary>
/// Debug UI.Text element
/// </summary>
public Text debugText;
#endregion
#region Constants
/// <summary>
/// Constant for decaying the velocity on updates
/// </summary>
private const float VELOCITY_DECAY_FACTOR = 0.85f;
/// <summary>
/// Constant to convert normal speed sizes to fit the scale
/// Of UnityEngine.Vector2
/// </summary>
private const float HUMAN_TO_VECTOR_SCALE_FACTOR = 850f;
/// <summary>
/// Constant to set the base speed of the player animation
/// </summary>
private const float BASE_ANIM_SPEED = 0.7f;
/// <summary>
/// Constant to slightly reduce the animation speed after
/// It is multiplied by the velocity of the player
/// </summary>
private const float POST_VELOCITY_MULTIPLICATION_ANIM_SPEED_FACTOR = 0.5f;
/// <summary>
/// Constant to set the animation speed
/// </summary>
private const float ANIM_SPEED_MODIFIER = BASE_ANIM_SPEED * POST_VELOCITY_MULTIPLICATION_ANIM_SPEED_FACTOR;
/// <summary>
/// Epsilon before velocity zerofication
/// </summary>
private const float VELOCITY_EPSILON = 0.1f;
#endregion
#region Private Members
private Rigidbody2D rb2D;
private Vector2 velocity;
private Animator animator;
private Directions dir = Directions.Idle;
#endregion
#region Game Loop Methods
private void Awake()
{
animator = GetComponent<Animator>();
rb2D = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
float vertical = Input.GetAxisRaw("Vertical");
float horizontal = Input.GetAxisRaw("Horizontal");
UpdateVelocity(horizontal, vertical);
UpdateAnimation(horizontal, vertical);
UpdateMovment();
}
#endregion
#region Animation Methods
private void UpdateAnimation(float horizontal, float vertical)
{
UpdateAnimation(new Vector2(horizontal, vertical));
}
private void UpdateAnimation(Vector2 input)
{
Directions direction;
if (input.y > 0)
direction = Directions.Back;
else if (input.y < 0)
direction = Directions.Front;
else if (input.x > 0)
direction = Directions.Right;
else if (input.x < 0)
direction = Directions.Left;
else
direction = Directions.Idle;
SetDirection(direction);
}
private void SetDirection(Directions value)
{
animator.SetInteger("Direction", (int)value);
dir = value;
}
#endregion
#region Movement Methods
private void UpdateMovment()
{
rb2D.MovePosition(rb2D.position + velocity * Time.fixedDeltaTime);
KinematicsDebugPrints();
ApplySpeedDecay();
}
private string GetDebugPrintDetails()
{
return string.Format("HOR : {0}\nVER : {1}\nDIR : {2}:{3}\nX : {4}\nY : {5}",
velocity.x,
velocity.y,
animator.GetInteger("Direction").ToString().PadLeft(2),
(Directions)animator.GetInteger("Direction"),
rb2D.position.x,
rb2D.position.y);
}
private void KinematicsDebugPrints()
{
var details = GetDebugPrintDetails();
debugText.text = details;
Debug.Log(details);
}
private void UpdateVelocity(float horizontal, float vertical)
{
if (vertical != 0)
velocity.y += Mathf.Sign(vertical) * speed / HUMAN_TO_VECTOR_SCALE_FACTOR;
if (horizontal != 0)
velocity.x += Mathf.Sign(horizontal) * speed / HUMAN_TO_VECTOR_SCALE_FACTOR;
animator.speed = ANIM_SPEED_MODIFIER * velocity.MaxOfXandY() ;
}
private void ApplySpeedDecay()
{
if (velocity == Vector2.zero) return;
velocity *= VELOCITY_DECAY_FACTOR;
velocity = velocity.ZerofiyTinyValues(0.1f);
}
#endregion
}
}
当前的播放器对象如下:
这是墙体对象(除图像外所有预设都相同):
这是我另一个错误的gif:
这是碰撞框和圆圈的样子:
这是检查器中的细节:
在与Hamza Hasan交谈后,他帮助我将所有外墙框碰撞器转换为四个连续的碰撞器,每个面一个(上,下,左,右)。
其代码位于BoardManager
脚本的CreateWallsColliders
方法中。
这是在场景编辑器中当前的样子:
UpdateVelocity
函数。 - AustinWBryan