我的性格有Rigibody2D和BoxCollider
如果使用Rigibody2D
,还必须使用BoxCollider2D
不BoxCollider
。确保墙壁也有BoxCollider2D
。
当transform.Translate
或transform.position
用于移动对象时没有发生碰撞。如果您的GameObject附带有Rigidbody2D ,则必须将其移动到Rigidbody2D.velocity
,Rigidbody2D.AddForce
(Rigidbody2D.AddXXX
)或Rigidbody2D.MovePosition
。
最好在FixedUpdate()
函数中做这个特殊的事情。此外,我认为应该使用GetAxisRaw
而不是GetAxis
,这样玩家将立即停止键/手指被释放。
public float speed = 2f;
Rigidbody2D rg2d;
void Start()
{
rg2d = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
float h = CrossPlatformInputManager.GetAxisRaw("Horizontal");
float v = CrossPlatformInputManager.GetAxisRaw("Vertical");
Vector2 tempVect = new Vector2(h, v);
tempVect = tempVect.normalized * speed * Time.fixedDeltaTime;
rg2d.MovePosition((Vector2)transform.position + tempVect);
}
如果速度太快/太慢,您可以随时减小/增加速度。
你的角色是否有BoxCollider2D(注意最后的2D)。你的墙也一样。 –
是的,它有BoxCollider2D – Sleeper
尝试使用rigidbody.MovePosition而不是transform.Translate。还要检查碰撞蒙版和物体图层 –