using UnityEngine;
using System.Collections;
public class asteroidMove : MonoBehaviour {
Vector3 MoveTo;
GameObject Asteroid;
bool final = false;
int sideStarter;
Vector3 initPosition;
float i = 0.0f;
void Start() {
initPosition = transform.position;
sideStarter = (transform.position.x == -200) ? 1000 : -200;
MoveTo = new Vector3 (sideStarter, 0, Random.Range (-100, -700));
final = true;
}
void FixedUpdate() { // Changing to Update() doesn't change anything.
if (final) {
transform.position = Vector3.Lerp(initPosition, MoveTo, i);
i += 0.001f;
if (transform.position == initPosition) { Debug.Log ("Finished!"); Debug.Break(); }
}
}
}
Script “works”. But too fast, as soon as I press “Play” button. The game is paused and Debug log says “Finished”. As it should, but no visible operation has been performed. Could someone lean me hand on this one?
It might be that i, adds up to 1 in 10000 steps within 1 frame. But do you have any idea how to make it slower?
Not really sure why you’re testing against the initial position of the object, instead of the target (MoveTo) position. That’s most likely the cause for why your code is ending early.
Try something like this, use Update instead of FixedUpdate:
void Update() {
if(final) {
i += Time.deltaTime;
transform.position = Vector3.Lerp(initPosition, MoveTo, i / 5); // change 5 to how many seconds you want it to take
if(transform.position == MoveTo)
{
Debug.Log("Finished");
}
}
}
Not really sure why you’re testing against the initial position of the object, instead of the target (MoveTo) position.
What?
Your code: transform.position = Vector3.Lerp(initPosition, MoveTo, i / 5);
My code:
transform.position = Vector3.Lerp(initPosition, MoveTo, i);