我有一个简单的自上而下的自行车游戏,我正在尝试增加转向功能。我想知道如何使用前轮的方向来确定自行车的方向和速度。
void Update ()
{
//Get input from user Vertical: 0 to 1, Horizontal -1 to 1
float forwardInput = Input.GetAxis("Vertical");
float sidewaysInput = Input.GetAxis("Horizontal") * m_steeringAmount;
// Turn front wheel
m_frontWheelTransform.localEulerAngles = new Vector3(0, sidewaysInput, 90);
// get speed and drag
float speed = m_velocity.magnitude;
Vector3 forwardDrag = -m_forwardDragConstant * m_velocity * speed;
// calculate acceleration
float engineForce = forwardInput * m_enginePower;
Vector3 forwardTraction = transform.forward * engineForce;
Vector3 forwrdForce = forwardTraction + forwardDrag;
Vector3 acceleration = forwrdForce / m_mass;
// update velocity and position
m_velocity += acceleration * Time.deltaTime;
transform.localPosition += m_velocity * Time.deltaTime;
}
我曾尝试将自行车速度应用于前后轮,并利用位置的差异来确定自行车的前进方向,但是向前的阻力使其变得混乱。
根据madshogo评论进行编辑