1.使用transform进行移动
强制移动,直接改变物体的位置,例如:
public float speed = 3;
Vector3 move;
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Move(h, 0, v);
}
void Move(float x, float y, float z)
{
move = new Vector3(x, y, z);
Vector3 to = transform.position + move; //要看向的目标点
transform.LookAt(to);
transform.position += move * speed * Time.deltaTime;
}
对于某些碰撞不好处理,比如说怼着墙走会发生抖动。
2.使用RigidBody进行移动
借助刚体组件移动,代码要写在FixedUpdate中,如:
public float speed = 3;
Rigidbody rigid;
Vector3 move;
void Start()
{
rigid = GetComponent<Rigidbody>();
}
void Update()
{
float h = Input.GetAxis("Horizontal");

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



