- Home /
Lerping issues (C #)
I'm attempting to move an object from one place to another, so I used a lerp but it isn't working.
Here's the function i created to run the lerp and also the instantiate:
attackObj = Instantiate (attackObj,attackingCity.transform.position,Quaternion.identity) as Transform;
attackObj.transform.position = Vector3.Lerp (attackingCity.transform.position,defendingCity.transform.position, Time.deltaTime);
Debug.Log ("Success!");
I'm running this function from update in an if statement for when I press space. It definately runs because the log works. It also does spawn the object on the attacking city, but doesn't move it at all. Any suggestions?
I've played around with the numbers a little and the position of the attackObj changes when I enter a value between 0 and 1. I haven't really used Lerp in a while so i'm a little rusty at it.
The final parameter needs to move from 0 to 1. At 0 it will return the start position, and at 1 it will return the end position.
Answer by Dave-Carlile · Nov 02, 2012 at 10:02 PM
Moving the t parameter in Vector3.Lerp from 0 to 1 over time will cause the object to move from the start position to the end over that same length of time.
Alternatively, you can pass in the position of attackObj as the start position, then use the t parameter as a damping value. So...
attackObj.transform.position =
Vector3.Lerp(attackObj.position,
defendingCity.transform.position,
10 * Time.deltaTime);
The value 10 here can be modified to control how fast the object moves. What this does is move the object from its current position to the target position by 10 Time.deltaTime* percent of the distance. As it gets closer it will move less of a distance. Increasing the multiplier will cause the object to move a greater percentage of the distance each frame, but it will stop much faster (i.e. not as smoothly) when it arrives.
Ah I see, i was using the attacking city's transform as the first argument rather than the attackObj's. Thanks for the help, it works fine now :D
Answer by Anxo · Nov 02, 2012 at 09:27 PM
When you are using attackObj = Instantiate, You are creating an instance of an object and naming it attackObj. If you are doing that in Update function, it will create an instance every frame. So it constnatly creates new transforms and names them attackObj.
When you are telling it to lerp, you are telling the attackObj to lerp but next frame the attackObj is a different object. So you are starting the lerp on one object every frame but never continuing the lerp on one object over multiple frames. If you move the instanciate into start you will see it lerp.
The code I typed above is in a function outside of the update which is only called when I hit space.
Also, will this be able to move the object smoothly, or is a lerp just for instant jumping from one place to another?
Your answer
Follow this Question
Related Questions
Getting parameter values sent to a method c sharp script 0 Answers
Error when trying to parse csv file 1 Answer
Trouble referencing variables inside of a script (C#) 1 Answer
Trouble setting a variable to a game object using instantiate. 1 Answer
Moving player in an arc, from startPoint to endPoint 2 Answers