- Home /
Problem With Mathf.Lerp
okay im trying to get my camera to lerp to an objects position.....i move the camera using this code....gameObject.transform.Rotate(Vector3(recoil, 0, 0));....so my camera is moved now im trying to get it to lerp back to another objects position....using this code...
var cameraToLerp : GameObject;
var lerpTime : float = 1;
function Update ()
{
this.gameObject.transform.rotation.x = Mathf.Lerp(this.gameObject.transform.rotation.x, cameraToLerp.transform.rotation.x, lerpTime);
}
and the camera doesnt move at all..the camera moves when i comment out the lerp command...but i dont know how to solve this and there are no tutorials that work.
There are serious issues here, and without more context I cannot tell you how to solve your problems. Transform.rotation is Quaternion...an non-intuitive 4D construct with values between -1 and 1. Don't manipulate the individual components unless you have a firm understanding of the math behind Quaternions. You can use Transform.eulerAngles ins$$anonymous$$d, but the manual states:
Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations.
In addition to the above problem, there are multiple eulerAngle representations for any give physical rotation, and Unity can (and does) change representation on you.
As for your Lerp() there are multiple different ways to use Lerp() and other functions like $$anonymous$$oveTowards() that can be used ins$$anonymous$$d. What you are doing here is not one of them. The last parameter either needs to vary from 0 to 1 over time, or it needs to be a small fraction. Typically 'speed * Time.deltaTime' is used and produces and eased rotation.
I'm sure there is an easy way to get what you want, but to do so I think you need to backup and describe in detail what this code needs to do...what is the relationship between the rotation of this game object and the cameraToLerp object.
All im trying to accomplish in this code is for the object to return back to the same rotation value as the other object...like the x,y,z rotation values....i need my object to return to those values over a certain period of time...im just having trouble finding the right tutorial for my problem.
never$$anonymous$$d i figured it out by putting time.deltatime for t...it worked just as i hoped...thanks anyways for trying to help me
Answer by Lairinus · Mar 11, 2014 at 03:22 AM
Going off of what Ben said, it seems you don't really understand what Lerp does.
I'll try my best to explain it, as I didn't fully understand it until recently.
When people talk of Lerping something, they mean to move an object from point A to point B over t Time, where t DOES NOT equal the actual "arrival time." t Will NEVER equal the arrival time, and will always be between 0 and 1. 0 is the start, and 1 is the end. (You need division here)
I'll use "arrival time" a lot in my explanation, but just know that arrival time means the total time that you want the number to go from its' start to its' finish (or its' 0 to 1)
I'll post my gravity script as an example:
IEnumerator Lerp_CurrentGravity()
{
_gravity.currentGravity = 0;
float totalTimeDelta = 0;
// Gradually Adjust Gravity
while (_gravity.currentGravity < _gravity.maxGravity && GetGravityState() == Gravity.State.Falling)
{
totalTimeDelta += Time.deltaTime;
_gravity.currentGravity = Mathf.Lerp(_gravity.currentGravity, _gravity.maxGravity, totalTimeDelta / _gravity.timeForMaxGravity); // Lerps the gravity
yield return new WaitForSeconds(Time.deltaTime);
}
if (GetGravityState() != Gravity.State.Falling)
_gravity.currentGravity = 0;
}
Look inside the While loop and you'll see a totalTimeDelta float. That represents the total time that you've been "falling" in this instance of my script.
Now if you know that your actual "arrival" time is 2, then you'll need to do the operation:
(totalTimeDelta / 2), which will give you an answer between 0 and 1.
This works for every sort of Lerping because its' all based around the same principal.
WHAT YOU'RE DOING:
You're setting the Lerp value directly to 1, which, is exactly the same as saying "Finish Lerping." You might as well use transform.rotation = new Quaternion(x) at that point.
Answer by benhumphreys · Mar 11, 2014 at 03:05 AM
Do you understand Mathf.Lerp
? If something is not working, read the documentation.
You need to change the third parameter between 0 and 1 to change the value returned by Lerp.
im trying to figure out a way for my object to slowly return to another objects rotation...it doesnt matter if im using lerp or not....i just need help figuring this rotation problem out.
Your answer
Follow this Question
Related Questions
Quaternion Rotation On Specific Axis Issue 0 Answers
Uniform rotation - uniform lerp 1 Answer
Rotate smoothly an object when key is up 0 Answers
Slowly Rotate to Look at 1 Answer
Flip over an object (smooth transition) 3 Answers