- Home /
Vector3.Slerp not working
Hello, I'm trying to write a script which creates a platform and over short period of time moves the platform into position above. But I can get it working properly: the platform instantiates only on start position and doesn't move at all. Thanks for the help.
using UnityEngine; using System.Collections;
public class GroundCreator : MonoBehaviour {
public GameObject[] ground;
public Transform startpoint;
public Transform endpoint;
public bool detector;
public float time = 1f;
void Start()
{
detector = false;
}
void OnTriggerEnter2D (Collider2D other)
{
if(other.tag == "Player")
{
detector = true;
}
}
void Update()
{
if(detector == true)
{
int i = Random.Range(0, ground.Length);
GameObject groundInstance = Instantiate(ground[i], startpoint.position, startpoint.rotation) as GameObject;
groundInstance.transform.position = Vector3.Slerp(startpoint.position, endpoint.position, time * Time.deltaTime);
Destroy(gameObject);
}
}
}
Answer by fschneider · Jul 04, 2014 at 01:54 PM
Don't use Time.DeltaTime as parameter to Slerp. If you want to animate the position for a given time, create one variable for defining the seconds you want to animate, and a second variable keeping track of the fraction of that you already animated. Then use a third variable to map the second variable to the interval [0..1] and pass that as a parameter to Slerp.
The use of deltaTime will work if the first parameter represents the current position, not the starting position. It produces and eased movement to the goal. So if 'groundInstance.transform.position was used for the first parameter, the object would move. If you don't want eased movement, you can still use deltaTime. Change the use of Slerp() to $$anonymous$$oveTowards() and also have the first parameter represent the current position.
Answer by meat5000 · Jul 04, 2014 at 02:41 PM
Lerp and Slerp are attended functions, i.e they require constant attention and must be called every frame to work properly.
groundInstance.transform.position = Vector3.Slerp(startpoint.position, endpoint.position, time * Time.deltaTime);
Destroy(gameObject);
You perform one step of Slerp and then destroy the object which handles it. Place your Slerp in a script on the Instantiated gameObject and use GetComponent to trigger the Slerp, so destroying your creator object won't prevent the Slerp being performed.