- Home /
Vector3.lerp doesn't work
Hi Everyone,
I'm just starting to learn Unity and I don't understand why this code doesn't work. i would create a cube and move it. The code run correctly but my cube doesn't move.
Thank you for your help
Julien
My code:
var cube;
var end : Transform;
function Start () {
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//cube.AddComponent(Rigidbody);
cube.transform.position = Vector3 (0, 0, 0);
end.transform.position = Vector3 (10, 0, 0);
}
function Update () {
cube.transform.position = Vector3.Lerp(transform.position, end.position, Time.deltaTime);
}
Are you trying to move the cube from the current object's position or from its own?
The last parameter of Lerp is a number between 0 and 1. 1 returns the end & 0 returns the start. Other values interpolate the positions.
The technique of using deltaTime works to update an object with smoothed slow down only when you are updating the start position - which you are not
cube.transform.position = Vector3.Lerp(cube.transform.position, end.position, Time.deltaTime);
This is what is meant. See here for a little explanation http://answers.unity3d.com/questions/339320/can-someone-help-me-to-understand-what-vector3lerp.html
This is more or less the same question that was recently answered here: http://answers.unity3d.com/questions/342154/lerping-issues-c-.html
Answer by JD · Nov 05, 2012 at 01:51 AM
Thank you all for your help, I've solved my problem.
var cube;
var end : Vector3;
function Start () {
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//cube.AddComponent(Rigidbody);
cube.transform.position = Vector3 (0, -10, 0);
end = Vector3(0, 0, 0);
}
function Update () {
cube.transform.position = Vector3.Lerp(cube.transform.position, end, Time.time/10);
//cube.transform.Translate(Time.deltaTime, 0, 0, Camera.main.transform);
}
Your answer
Follow this Question
Related Questions
Stop a Lerp from looping 3 Answers
CharacterController Gravity 2 Answers
Lerp not working 1 Answer
ricreate Lerp function 2 Answers
JS access spherecast position 1 Answer