- Home /
Vector3.lerp doesn't work when level is loaded
I've been having having a big problem with lerp. I though it was a problem with the builder, but through trial and error, I discoverd that the lerp function doesn't work when a level is loaded. If the game starts on the level, it's fine, but when I use Application.LoadLevel("alevel"); the lerp function doesn't work.
I also don't think it is directly to do with my code because I've used something as complex as this:
public class Upgrades : MonoBehaviour {
public lives script;
public AudioClip blip;
bool on;
void Awake(){
renderer.enabled = false;
on = false;
}
void OnMouseEnter()
{
transform.localScale = new Vector3(0.16f, 0.16f, 1f);
if(on == true){
audio.PlayOneShot(blip, 1.0F);
}
}
void OnMouseExit()
{
transform.localScale = new Vector3(0.12f, 0.12f, 1f);
}
void Update() {
if(lives.livesleft <= 0){
renderer.enabled = true;
on = true;
}
if(player.paused == true){
renderer.enabled = true;
on = true;
}
else if(player.paused == false && lives.livesleft >= 1)
{
renderer.enabled = false;
on = false;
}
}
void OnMouseDown()
{
if(on == true){
Application.LoadLevel("Shoot stuff in space");
}
}
}
To as basic as this:
public class test : MonoBehaviour {
void Update () {
transform.position = Vector3.Lerp(transform.position, new Vector3(100.0f, 20.0f, 1.0f), 10f * Time.deltaTime);
}
}
And the result is the same. I tried different game objects, different scenes, different scripts but the level being loaded is the only thing in common so far.
I'm loosing my mind so any help would be fantastic.
I took your basic script and tested it. It worked fine on the load of a new level. I don't see a Lerp() in your first script.
I uploaded the wrong script for the first one. $$anonymous$$aybe something is wrong with my computer for all I know; it beats me why it's not working. I took supernats advice, and it turns out that the proper vector 3 lerp works. Thanks though.
Answer by supernat · Nov 20, 2013 at 05:14 AM
I think the problem is that you are using Time.deltaTime as your lerp factor. Maybe you meant to do this, but I can't see how this would ever be useful to set a transform position. DeltaTime is the amount of time passed since the last frame was run, and it fluctuates. The lerp factor should always be 0.0 to 1.0. I would take another look at what you are trying to accomplish and choose a different approach.
@supernat - The use of Time.deltaTime as a Lerp factor is a common approach and is used frequently in answers on UA. It works because the current position (transform.position in the code above), is updated each frame. The result is that the object moves approximately the same fractional amount towards the destination each frame, but since the distance is shrinking, the distance moved each frame is shrinking. The result is an eased movement towards the destination. Note this approach might fail if there is a large deltaTime on the load of a scene since the "fraction" might be larger than 1.0. A quick hack to test if this is an issue would be to not do the Lerp() if deltaTime is above some threshold...say .01 and see what happens.
Ahh, I see your point, never considered using it that way. I'll have to keep this trick in $$anonymous$$d, I usually add easing the hard way.
Your answer
Follow this Question
Related Questions
Move Transform to Target in X seconds 3 Answers
make my gameobject lerp between points more smoothly 0 Answers
Vector3 Lerp mis-match 0 Answers
[Solved] PlayerPrefs Vector 3 Defaulting to 0,0,0 1 Answer
moving between two points over time 1 Answer