车辆的真实转弯
本文关键字:转弯 真实 | 更新日期: 2023-09-27 18:27:21
我正试图为一辆简单的汽车添加逼真的驾驶行为。目前,我使用刚体MovePosition,并且只有在从Input检测到正向/反向轴时才应用MoveRotation。
相反,即使汽车在滑行,我也希望应用方向/旋转,但不希望它在静止时转弯,就像它是一辆履带车辆一样。我还希望转弯看起来来自车轮(而不是游戏中的实际物体)所在的车辆前部。
到目前为止,我所拥有的大多是从坦克教程中借来的:
m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
m_TurnInputValue = Input.GetAxis(m_TurnAxisName);
// Create a vector in the direction the tank is facing with a magnitude based on the input, speed and the time between frames.
Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;
// Apply this movement to the rigidbody's position.
m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
if (m_MovementInputValue > 0)
{
float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;
// Make this into a rotation in the y axis.
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
// Apply this rotation to the rigidbody's rotation.
m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
}
我认为您可以通过查看Unity API的轮式对撞机组件来节省一些时间:http://docs.unity3d.com/Manual/class-WheelCollider.html,我以前看过一个演示,它用来制作一个完整的赛车游戏,所以它应该能够模拟逼真的车辆(有问题的演示)。
除此之外,以下是如何做到这一点:
首先,如果您希望车辆仅在向前或向后移动时转弯,您可以将转弯力乘以车辆的速度向前矢量
float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime * (m_Rigidbody.velocity.magnitude / MaxVehicleSpeed);
(m_Rigidbody.velocity.magnitude/MaxVehicleSpeed)的想法是根据车辆速度,使值介于0和1之间。
然后为了让转弯从车辆前方出现,您需要更改旋转点,而我无法使用MoveRotation找到解决方案。您可以使用刚体的AddForceAtPosition函数来实现这一点,但计算起来会变得更加复杂。
想法如下:
Vector3 force = transform.right * turn;
m_Rigidbody.AddForceAtPosition(force, MiddleOfWheelAxis);
使用MiddleOfWheelAxis两个前轮之间的点。