Unity5 实现汽车类物体转向的方法

Unity中,汽车的转向通过向前移动,并且同时绕y轴旋转进行。通用方法是:

RigidBody m_RigidBody = GetComponent<RigidBody> ();
float speed = 6f;

private void FixedUpdate()
{
	Move();
	Turn();
}

private void Move()
{
	float v = Input.GetAxis ("Vertical");
	// 注意这里的forward
	Vector3 movement = transform.forward * v *speed * Time.deltaTime;
	m_RigidBody.MovePosition (m_RigidBody.position + movement);
}

private void Turn()
{
	float h = Input.GetAxis ("Horizontal");
	float turn = h * speed * Time.deltaTime;
	Quaternion turnRotation = Quaternion.Euler (0f, turn, 0f);
	m_RigidBody.MoveRotation (m_RigidBody.position + turnRotation);
}
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页