- Home /
Mathf.Lerp not working
Hi, I have been having this issue for a while now and finally decided to make a question about this since it's happenning in a lot of situations and not just only this one, the thing is, I can't get Mathf.Lerp to work, nor Vector3.Lerp, any kind of Lerp, I am trying to lerp the rotation of an object in the y axis. But I've been trying to do this in a lot of different situations, like lerping between two positions. The code just doesn't work.
this is the line of code, it is on LateUpdate:
theRotation.y = Mathf.Lerp (transform.rotation.y, cameraTargetScript.currentYRotation, Time.deltaTime * rotateSpeed);
So, what am I doing wrong? Thanks in advance.
Answer by whydoidoit · Mar 01, 2014 at 09:38 AM
Well your problem there is that you are lerping a part of a quaternion which is not the y rotation you probably think it is.
var someRotation = transform.eulerAngles;
someRotation.y = Mathf.LerpAngle(someRotation.y, cameraTargetScript.currentYRotation, Time.deltaTime * rotateSpeed);
transform.eulerAngles = someRotation;
Note use LerpAngle for angles as it goes the right way around (the shortest route). You have to put the rotation back into the transform and if you want actual degrees use eulerAngles not rotation.
Okay... now this is gettin weird, something seems to be wanting the rotation to snap back
Oh, I didn't read your script well, so it should also work, anyway just Lerp seems to be working fine
Answer by SirCrazyNugget · Mar 01, 2014 at 09:54 AM
From the looks of your code Lerp isn't the function you're looking for.
Lerp interpolates between two values by a value between 0f and 1f. If you specify the two values from=60 and to=100 depending on the value given will return the interpolated value in between those two points.
so:
Mathf.Lerp(60f, 100f, 0f); //return 60f
Mathf.Lerp(60f, 100f, 0.25f); //return 70f
Mathf.Lerp(60f, 100f, 0.5f); //return 80f
Mathf.Lerp(60f, 100f, 0.75f); //return 90f
Mathf.Lerp(60f, 100f, 1f); //return 100f
and obviously everything in between.
Looks like he is doing a damped lerp to me - nothing wrong with that.
since the code runs everyframe, it should work like a damp anyway