Rotate Object based on distance to destination
I am making a runner game where the player ship switches lane by the swipe of the player.
I would like make the player ship bank left and right during this lane switch, and also return to normal by the time it reaches it's destination. Here is a diagram that shows what I am looking to accomplish: Diagram
I have calculated the distance of between the current lane and the destination lane. I have also calculated the current distance of the player ship while it is switching lanes. I have messed with RotateTowards and transform.rotation, but I have NO clue on how to apply this to rotation equation to get the result I am looking for.
Any help pointing me in the right direction would be greatly appreciated. Thanks!
Answer by taylank · Jan 10, 2016 at 03:49 AM
You will likely want to use the Quaternion.Lerp method. The Lerp method returns a linearly interpolated value between the to and from values, based on the t variable you supply. So if I do Lerp(0, 100, 0.5), I'd get 50. If I used .75 I'd get 75, etc.
Your "from" value is the neutral rotation you want when the ship is not banking. The "to" value is the maximum angle you want the ship to tilt to.
The target rotation at maximum tilt can be expressed as a Quaternion with:
// assuming 65 degrees is the maximum tilt you want
var targetRot = Quaternion.AngleAxis(65, shipTransform.forward);
Based on where you are in between the lanes, you will lerp back and forth. When you are on either lane, you will want
transform.rotation = Quaternion.Lerp(defaultRotation, targetRot, 0);
When you are right in the middle of two lanes, you will want
transform.rotation = Quaternion.Lerp(defaultRotation, targetRot, 1);
For anywhere else, you need to express the current location of the ship as a fraction of the distance to the middle, and use that as the last variable in the Lerp method.
For instance, at 25% and 75% marks, you would use 0.25f;
You can also experiment with Quaternion.Slerp to see if it looks better.
With the use of a $$anonymous$$athf.Sin this has worked beautifully. Thanks!
Your answer
Follow this Question
Related Questions
Ambient Emitter inside a BoxCollider - Rotation not recognized 0 Answers
Why does RotateTowards rotate in an "orbit" rather than on the spot? Help me understand! 0 Answers
Rotation in an if statement 1 Answer
Changing player co-ordinates 0 Answers
Rotating a 3D object with movement along a 2D background at an angle 1 Answer