- Home /
Unity 3D - Rotating an object in relation to its velocity
I am currently trying out Unity 3D and I've run into a problem. The problem is I have a car that moves along a road and then when it reaches a corner I would like it to turn the corner and have the model rotate based on its current velocity.
Here is the code I have for moving the object forward:
_velocity += (_velocity * Time.deltaTime) + (_acceleration / 2f) * Time.deltaTime * 2f;
_velocity = Mathf.Clamp(_velocity, 0f, MaxVelocity);
Vector3 position = this.transform.position;
position += -this.transform.forward * _velocity * Time.deltaTime;
this.transform.position = position;
The variables are declared as class members:
private const float MaxVelocity = 10;
private float _velocity;
private float _acceleration;
This appears to work OK for moving forward. If I applied a rotation it may look OK for a certain velocity, but if the velocity changes, then the rotation seems to quick or to slow.
What I would like (but completely failing at accomplish) is when it comes time to turn, then I would look at the velocity and calculate the rotation accordingly so when an object performs a turn it will always end up at the same point regardless of how quick it approaches the corner.
I have tried a number of options, over countless hours, but I'm not having any luck!
Any ideas?