- Home /
Loop between points with Vector3.slerp
I have my gameobject moving between points just fine. The problem that I'm encountering is the speed at which they are moving between points. When it moves to the first Vector3 the first time the speed is fine but every time after that its going straight to the point. I know its because timer is set to += time.deltatime. But whenever I try to not to use delta time or mess with other speed forms it just cause my object to shake in place. Is there a way that I can just have a constant speed between each points? Here is my code.
using UnityEngine;
using System.Collections;
public class Pendulum : MonoBehaviour {
private Vector3 endPos = new Vector3 (2.0f, 1f, 1f);
private Vector3 startPos = new Vector3(-2.0f, 1f, 1f);
public float Timer = 0;
public bool startMoving = true;
void Start()
{
transform.position = startPos;
}
void Update()
{
//startMoving = true;
if(startMoving)
{
Timer += Time.deltaTime;
transform.position = Vector3.Slerp(startPos, endPos, Timer);
StartCoroutine("BackTime");
}
if(startMoving == false)
{
Timer += Time.deltaTime ;
transform.position = Vector3.Slerp(endPos, startPos, Timer);
StartCoroutine("ForwardTime");
}
}
IEnumerator BackTime()
{
yield return new WaitForSeconds(2);
startMoving = false;
}
IEnumerator ForwardTime()
{
yield return new WaitForSeconds(2);
startMoving = true;
}
}
Answer by TimBorquez · Nov 05, 2013 at 09:50 PM
im not sure if this is exactly what you're lookin for but i have used code like this to loop between two points at a constant speed:
someObject.transform.position = Vector3(Mathf.PingPong(Time.time*5, 35), someObject.transform.position.y, someObject.transform.position.z);
just changing the ping pong values should effect speed and distance (im only moving it on the x in the example) (also i think it has to be in a loop or update function)
Answer by Professor Snake · Nov 05, 2013 at 10:33 PM
Just replace Timer with Time.deltaTime or reset Timer to 0 after one stage is finished. The third argument in Slerp is something that should range between 0 and 1 and determines the stage of the interpolation. Vector3.Slerp(x,y,0) will return x, and Vector3.Slerp(x,y,1) will return y. After the first interpolation is finished Timer's value is greater or equal to 1, making the rest of the interpolations instant.
Answer by Dracorat · Nov 06, 2013 at 06:20 PM
Fully documented for your reading pleasure...
using UnityEngine;
using System.Collections;
/// <summary>
/// Moves the attached transform from one Vector3 position to another and back again,
/// over a specified time interval.
/// </summary>
public class Oscillate : MonoBehaviour {
/// <summary>
/// Whether we should move like a Sawtooth animation, with suddent turn arounds at the end
/// or like a sine wave with the ends "rounded" for smoother animation.
/// Sine will not maintain a straight line - so if you want a straight line, stick with Sawtooth
/// or hard code an axis in to the result.
/// </summary>
public enum MovementTypes {
Sawtooth,
Sine
}
//Inspector Variables. These should be assigned in the inspector, but can be assigned by script at run time.
public Vector3 StartPosition = new Vector3(-2, -2, -2);
public Vector3 EndPosition = new Vector3(2, 2, 2);
public float TimeToMoveBetweenPoints = 1f;
public MovementTypes MovementType = MovementTypes.Sawtooth;
//These variables are used to maintain state as the program runs.
private float startTime;
/// <summary>
/// When the script begins, it needs to store the elapsed time in order to "zero" the animation.
/// </summary>
void Start () {
this.startTime = Time.time;
}
/// <summary>
/// Each frame, we need to check the current time and apply the oscillation to the transformation
/// </summary>
void Update () {
// We "wrap" the results over TimeToMoveBetweenPoints * 2 because we move to and then move back
// which is twice the movement.
float timeOffset = (Time.time - this.startTime) % (this.TimeToMoveBetweenPoints * 2);
// We will need a variable to store the percent through an animation we are
float percent = 0f;
// If the time is less than or equal to TimeToMoveBetweenPoints, we are on the first
// "half" of oscillation - or oscillation "out" - otherwise we are on the second half
// - or oscillation "in"
if(timeOffset <= TimeToMoveBetweenPoints){
percent = timeOffset / TimeToMoveBetweenPoints;
if(MovementTypes.Sawtooth == this.MovementType){
this.transform.position = Vector3.Lerp(StartPosition, EndPosition, percent);
}else{
this.transform.position = Vector3.Slerp(StartPosition, EndPosition, percent);
}
}else{
percent = (timeOffset - TimeToMoveBetweenPoints) / TimeToMoveBetweenPoints;
if(MovementTypes.Sawtooth == this.MovementType)
{
this.transform.position = Vector3.Lerp(EndPosition, StartPosition, percent);
}else{
this.transform.position = Vector3.Slerp(EndPosition, StartPosition, percent);
}
}
}
}