- Home /
How to steer a vehicle
Hello, I'm making a racing game from scratch. The vehicle currently has a working throttle and brakes. The issue is that I tried several techniques to to make it steer but I couldn't.
With the following code I can rotate the vehicle's position:
Vector3 steer_rotation3 = new Vector3 (0.0f, steer_angle, 0.0f);
transform.Rotate (steer_rotation3);
So at each step of the engine, the car rotates by "steer_angle" degrees (it rotates absolutely, not relative to the floor, but anyway). The issue is that the velocity of the vehicle does not change, it keeps moving the same direction as before.
So I tried to set a new speed manually, by taking rigidbody.velocity
and rotating it by steer_rotation3
. That can be done with Quaternion.FromToRotation()
, but it returns a Quaternion, not a Vector3 as required by rigidbody.velocity
. And the Vector3.RotateTowards() seems like another completely different thing.
Another completely different idea was to change rigidbody.rotation
by rotating it by "steer_angle" degrees. I tried the following commands but neither worked:
// auxiliary variable
Quaternion steer_rotation4 = Quaternion.Euler(0, steer_angle, 0);
rigidbody.rotation = Quaternion.RotateTowards (rigidbody.rotation, steer_rotation4, steer_angle);
rigidbody.rotation = Quaternion.FromToRotation (rigidbody.transform.forward, steer_rotation3);
rigidbody.rotation.SetFromToRotation (rigidbody.transform.forward, steer_rotation3);
Any suggestions? Thanks!
Answer by JinJin · Mar 13, 2015 at 06:24 PM
use
Vector3 originalVelocity;
Quaternion rotation;
Vector3 resultVelocity = rotation * originalVelocity;
to rotate originalVelocity by rotation;