我正在储存自己的velocity
载体,然后我使用transform.Translate
进行应用。这显然忽略了任何碰撞检测,并且需要定制碰撞检测实现。
相反,我发现Rigidbody
组件包含它自己的velocity
变量。该速度值可以很容易地改变,物体会自动转换并与障碍物碰撞。这里有一个例子:
using UnityEngine;
using System.Collections;
public class CharacterController : MonoBehaviour {
public Vector2 maximumSpeed = new Vector2(1.0f, 1.0f);
void Start() {
}
void Update() {
Rigidbody rigidbody = GetComponent<Rigidbody>();
Vector2 velocity = new Vector2();
velocity.x = Input.GetAxis("Horizontal") * maximumSpeed.x;
velocity.y = Input.GetAxis("Vertical") * maximumSpeed.y;
rigidbody.velocity = velocity;
}
}
这似乎工作得很好。将不胜感激:-)
也许更适合玩家控制的实体的另一种解决方案是使用CharacterController组件,该组件提供了类似的功能,但具有一些“松散的”逻辑以允许实体是由玩家控制的(相反成为一个纯粹的物理实体)。尽管你的速度/惯性,但你可能最适合你的解决方案。 – 2012-01-30 18:33:54
@Dan Cheers我会试验一下。 “CharacterController”可能表现更好吗? – 2012-01-30 18:41:59