Thursday, November 3, 2016

unity - How can i rotate the FPSController to it's original rotation angle?


It should be kind of slowly standing effect. The FPSController is starting at rotation: X = 7 Y = 112 Z = -31


Rotation


The problem is that i have on the FPSController a script that i use the mouse to rotate the camera around. And i see that when i rotate the camera the center point of the FPSController seems stuck on this angle even if i change the rotation to 0,0,0


If i start the game running it when the rotation of the FPSController is 0,0,0 then it's standing but when i start the game with 7,112,-31 and then moving the mouse it keep be locked on this rotation values. And i want it to slowly return to the original 0,0,0



I tried when the game is running and the fpscontroller rotation set to 7,112,-31 to change it to 0,0,0 but it keep staying at the rotation of 7,112,-31


What i want is that the fpcscontroller to stand. I don't think that i need to change it's position but only the rotation.


In my script i tried this: At the top:


public FirstPersonController fpc;
public float speed = 10F;

Then in the Update:


void Update()
{
fpc.transform.rotation = Quaternion.Slerp(fpc.transform.rotation,

Quaternion.Euler(0, 0, 0), Time.deltaTime * speed);
}

But it didn't return it to the standing position it keep it on the current rotation 7,112,-31



Answer



I think I didn't get quite right your issue, but I'll try...Maybe you could store the initial rotation on Awake:


Edit: Now I got your issue. You are using the interpolation (Slerp) wrong. It is based on the formula:



P = P0 + (t * (P1 - P0)), 0 < t < 1




So it is an interpolation normalized between 0 and 1, the problem with your function is that (Time.deltaTime * speed) is always giving a delta of frames duration (usually 0.02) multiplied by your speed, that is never going to reach 1 to have your rotation at the Quaternion.identity. You should play with a float that each frame gets summed until it reaches 1, giving a quick example:


Quaternion initialRotation; /// Here you store the initial rotation.
float t = 0.0f; /// Time reference for the interpolation.

/// Called on MonoBehaviour initialization.
void Awake()
{
initialRotation = transform.rotation;
}


/// This MonoBehaviour's Tick at each frame.
void Update()
{
transform.rotation = Quaternion.Slerp(initialRotation, Quaternion.identity, t);
t += (Time.deltaTime * speed);
}

So you could use that value initialized on Awake, the slerp takes that rotation, since interpolating between the actual rotation wouldn't give the expected results. We are using Quaternions here, since what there is shown on the inspector is an Euler Angle representation of the quaternion, and its less recomended to play with eulerAngles. So, if you want your FPC to be at a (0,0,0) rotation, then set its transform.rotation to Quaternion.identity.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...