- Home /
Vector3.Lerp not working?
I have a Vector3.Lerp in an Update function but it doesn't seem to move my object. This is my script:
private var elfMale;
public var smooth = 2;
private var newPosition;
function Awake ()
{
//newPosition = elfMale;
}
function Start ()
{
elfFemale = GameObject.Find ("FemaleElf").transform.position;
elfMale = GameObject.Find ("MaleElf").transform.position;
}
function Update ()
{
if (move)
{
newPosition = new Vector3 (10, 10, 0);
elfMale = Vector3.Lerp (elfMale, newPosition, Time.deltaTime * smooth);
}
}
I used the Unity Official Tutorials video and they put: newPosition = elfMale in the Awake function but I've commented it out since it seems pointless for me. Any ideas of why this isn't working? Also, I've activated the move variable through a GUI button.
$$anonymous$$y first hunch is that the variable can't find the $$anonymous$$eleElf GameObject. First check that the gameobject isn't returning null.
I'm pretty sure getting the elf$$anonymous$$ale.transform.position would just copy its values into the variable ins$$anonymous$$d of having the variable actually point to the position. This is something that doesn't happen with Transform, though, so try:
elf$$anonymous$$ale = GameObject.Find ("$$anonymous$$aleElf").transform;
elf$$anonymous$$ale.position = Vector3.Lerp (elf$$anonymous$$ale.position, newPosition, Time.deltaTime * smooth);
I think I actually tried this previously and it gave me the same error as I just got now:
"'position' is not a member of 'Object'."
Answer by MikeNewall · May 04, 2014 at 07:08 PM
Your code in the start function:
elfFemale = GameObject.Find ("FemaleElf").transform.position;
Is getting the position when the script starts, but you need to get a reference to the objects transform so you can translate it.
elfFemale = GameObject.Find ("FemaleElf").transform;
elfMale = GameObject.Find ("MaleElf").transform;
Then in Update you can go:
elfMale.position = Vector3.Lerp (elfMale.position, newPosition, Time.deltaTime * smooth);
Answer by rutter · May 06, 2014 at 11:31 PM
You're misunderstanding the parameter to Lerp
. I'll quote an old forum post of mine to explain.
I usually just explain the lerp parameter as a percentage. Given a
t
from 0 to 1, you'll get a result that ist
percent between a and b.If you want a value that changes over time,
t
needs to change over time.
This example is in C#, but the fundamental considerations are identical:
public float min = 0f;
public float max = 100f;
public float lerpTime = 1f;
float currentLerpTime = lerpTime + 1f;
public void StartLerping() {
currentLerpTime = 0f;
}
void Start() {
StartLerping();
}
void Update() {
currentLerpTime += Time.deltaTime;
if (currentLerpTime <= lerpTime) {
float perc = currentLerpTime / lerpTime;
float current = Mathf.Lerp(min, max, perc);
Debug.Log("current: " + current);
} else {
Debug.Log("finished");
}
}
In your case, you're passing current Time.deltaTime
to Lerp
. That's only the time for the current frame. The above example instead uses a value which increases by Time.deltaTime
once per frame, representing the total time that has passed since we started lerping.
@rutter - the OP's original use of Lerp() is common and will work as long as the starting position is updated each frame. It results in an eased movement. It does it job by moving nearly the same fraction towards the goal each frame. Since the goal is getting closer each frame, the actual movement shrinks. The result is an eased movement. One downside of the OP's form of Lerp is that, though it gets close quickly, it takes a long time to reach the goal (start and end positions are deemed equal by Unity). It is like the old though question, if you move half the distance to your goal each day, how long does it take you to reach your goal. Answer: never. Your use of Lerp() is the traditional one and results in linear movement.
Answer by vipinsoni38 · Mar 03, 2020 at 07:53 AM
public class CameraScript : MonoBehaviour
{
// Start is called before the first frame update
bool isGoing;
float starttime,fraction,time;
Vector3 a, b;
void Start()
{
isGoing = false;
fraction = 0;
}
// Update is called once per frame
void Update()
{
if (isGoing)
{
fraction = (Time.time - starttime) / time;
Debug.Log(fraction + " a: " + a + " b: " + b);
if (fraction > 1)
{
isGoing = false;
return;
}
transform.position = Vector3.Lerp(a, b, fraction);
}
}
public void LerpIt(Vector3 a, Vector3 b, float time)
{
Debug.Log("Entered");
starttime = Time.time;
this.a = transform.position;
this.b = b;
this.time = time;
isGoing = true;
//transform.position = b;
}
}
in my case, I was simply writing Vector3.Lerp();
the mistake was transform.position = vector3.lerp();