- Home /
My Lerp() is not working
Ok, so I have a game where coins are created and fall down, if you click on them they disappear. But, if one reaches the line at the bottom, it's GameOver. So I can correctly test for a coin hitting the line, but after that I want the coin to go to 0,0,0 so it can play a cool animation that I have yet to do. So, I created the script below and the position = Lerp() is not working, the coin just stays where it is. I don't get any errors, it just stays where it is. Hope you can help! Thanks!
#pragma strict
var CoinCreater : CoinCreater;
function Start () {
}
function Update () {
}
function OnTriggerEnter(object : Collider){
if(object.gameObject.tag == "Coin"){
var CoinFall : CoinFall = object.GetComponent("CoinFall");
CoinFall.enabled = false;
CoinCreater.scriptEnabled = false;
CoinCreater.enabled = false;
var extraCoins : GameObject[];
object.gameObject.tag = "GameoverCoin";
extraCoins = GameObject.FindGameObjectsWithTag("Coin");
for(var currentCoin in extraCoins){
Destroy(currentCoin,0.0);
}
var rate : float = 0.05;
var destination : Vector3 = new Vector3(0, 0, 0);
var position : Vector3 = object.gameObject.transform.position;
position = Vector3.Lerp(position, destination, rate * Time.deltaTime);
}
}
Answer by Eric5h5 · Feb 08, 2014 at 04:45 AM
Lerp isn't a function that "magically does stuff over time", it's a very basic math function that just returns a value once, immediately, like all other math functions. The third parameter is a value from 0 to 1, like a percentage, where 0 returns the first parameter, 1 returns the second parameter, and values in between return a percentage between the two. If you want to animate with Lerp you must call it repeatedly every frame while gradually advancing the third parameter from 0 to 1.
Answer by johnnyjimjams · Feb 08, 2014 at 05:13 AM
This line:
var position : Vector3 = object.gameObject.transform.position;
creates a copy of the current gameobjects position.... not a reference to it.
So when you change 'position' in the next line, you are only changing your fresh copy of it. 'position' is not attached to your gameObject.
You most likely want:
transform.position = Vector3.Lerp(position, destination, rate * Time.deltaTime);
Your answer
Follow this Question
Related Questions
Make Lerp or other more fluid or continuous 3 Answers
Using lerp to scale 1 Answer
What is interfering with my Lerp? 1 Answer
Moving multiple objects at same speed 1 Answer
Smooth the Changes in TimeScale, in TimeBending script 0 Answers