I am doing a Unity 3D project like temple run. The problem is that when a turns arrives , the game object need to turn 90 degree smoothly i.e. during the right swipe the game object will turn 90 degree right and during left swipe the game object moves 90 degree left smoothly. Please help.
2 Answers
2I have solved my problem that I am facing. I have used the following function.
function Rotation (thisTransform : Transform, degrees : Vector3, time : float) {
var startRotation = thisTransform.rotation;
var endRotation = thisTransform.rotation * Quaternion.Euler(degrees);
var rate = 1.0/time;
var t = 0.0;
while (t < 1.0) {
t += Time.deltaTime * rate;
thisTransform.rotation = Quaternion.Slerp(startRotation, endRotation, t);
yield;
}
}
Just I call the function like this:
Rotation(transform,Vector3(0,-90,0),0.3);
This means that my game object will move 90 degree left direction and it will take 0.3 second to move.
Don’t Temple-Run-like games immediately turn (i.e. not smoothly)? Without seeing your movement code, it is difficult to suggest an approach. Here is a list of methods that are commonly used as part of a smooth rotation:
this will not work if your character speed is increasing with time
– hariszaman