- Home /
Counter clockwise rotation with lerp
Hy guys
I've two condition of return of my gameobject to original rotation (0,0,0); i 've a problem with the second bool (destra) because i would like that the return is counter clockwise and not clockwise.
Any suggestion or alternative?
Thank you
else if (sinistra==false)
{
Vector3 destination= new Vector3(0, 0, 0);
transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles,destination,Time.deltaTime);
}
else if(destra==false)
{
Vector3 destination = new Vector3(0, 0, 0);
transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, destination, Time.deltaTime);
}
I am tired of explaining the problem over and over again, but I found this article
Answer by highpockets · Apr 15, 2019 at 05:02 PM
You are using Time.deltaTime to return a very similar Vector3 every time you call the lerp function. If your game has a steady frame rate, the returned Vector3 will actually have a small change to begin with and then stay that way. The last parameter of lerp takes 0.0 to 1.0 as an input parameter. If you input 0.0, the first Vector3 parameter you pass is returned, if you pass 1.0, the second Vector3 parameter is returned. What you are doing is passing the time in seconds passed since the last frame, let’s say you have 30 FPS, well you pass the function 1 / 30 = 0.03333333 every time you call the function so the interpolation is 3 percent finished every time you call it. You need to update the time value.
float timer = 0.0f;
void Update(){
transform.position = Vector3.Lerp (startPos, endPos, timer);
timer = timer + Time.deltaTime;
}
I also notice that you are using transform.rotation.eulerAngles, that should be transform.eulerAngles. transform.rotation is a quaternion and eulerAngles is a Vector3 that you use to rotate in degrees.
I failed to give you a good suggestion for this. To ensure that you get a counterclockwise rotation, I would think it would be best to use something like transform.Rotate() or calculate the quaternion rotation. Euler angles should just be used to set the angles to absolute values, but you are interpolating. Euler angles also fall victim to gimbal lock. One other issue with your question is that Vector3(0,0,0) as a Euler value might tell us that your object’s start rotation, but we have no idea what axis you want to turn counterclockwise/clockwise on. But since you know that when destra == false you want to turn counterclockwise to your original rotation. So if the axis that you want to rotate on is the y axis and you get the Euler angle first of all:
eulerYAngle = transform.eulerAngles;
You need to know how fast you want to do the rotation. Let’s say, you want 1 second to pass every 180 degrees of rotation.
Let’s say clockwise is positive and counterclockwise is negative.
So when you want to go clockwise:
eulerYAngle = 360-eulerYAngle;
Counterclockwise would be:
eulerYAngle = -eulerYAngle;
Your timer will have to be set:
timer = $$anonymous$$athf.Abs(eulerYAngle)/180; //set every 180 degrees to 1 second for consistency
Now apply the rotation:
void Update(){
if( timer > 0.0f ){
transform.Rotate(0,eulerYAngle * Time.deltaTime,0);
timer = timer - Time.deltaTime;
}
}
//this will rotate around the y axis by the angle in degrees until the timer runs out which should be almost exact. To be certain to be exact, you can manually set the value when timer is <= 0.0f.
Hope that helps