Add this class to your project. It’s in javascript but it should be trivial to convert to C# if you need. This isn’t the only (or even the bestest) way to do it but it should get the job done.
Use it by starting a timer sequence by creating a variable in start / awake or where ever you start the thing ping ponging.
var myToggle = new ToggleBackForth(-6.6f, 0.57f, 1.0f);
In your update code your transforms x value would then be myToggle.GetCurrent()
transform.position = new Vector3(myToggle.GetCurrent(), transform.position.y, transform.position.z);
When your done toggling back and forth call
myToggle.StopTimer();
ToggleBackForth.js
//toggle between start value and end value given a duration
class ToggleBackForth
{
private var done : boolean;
private var current : float ;
private var stopat : float;
private var buffer : float;
private var toggleTime : float;
//default constructor if using this you need to manually start the timer using StartTimer()
function ToggleBackForth()
{
done = false;
current = -6.6f;
stopat = 0.57f;
buffer = current;
toggleTime = 5.0;
}
function ToggleBackForth(start : float, end : float, duration : float)
{
done = false;
current = start;
stopat = end ;
buffer = current;
toggleTime = duration;
StartTimer();
}
function StartTimer()
{
while(!done )
{
buffer = current;
current = stopat;
stopat = buffer;
yield WaitForSeconds (toggleTime);
}
}
function StopTimer()
{
done = true;
}
function GetCurrent():float
{
return current;
}
}
This is a special case scenario in Unity when C# scripts attempt to access Javascripts. C# scripts compile first and won’t know about javascripts.
I’ve shamelessly copied this from here : http://unitygems.com/mistakes1/
SomeBehaviourScript.cs <-goes on the object that will move
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
ToggleBackForth myToggle;
// setup the toggle to make a move ever 1.0 seconds
void Start ()
{
myToggle = new ToggleBackForth(-6.6f, 0.57f, 1.0f);
StartCoroutine(myToggle.StartTimer());
}
// Update is called once per frame
void Update ()
{
transform.position = new Vector3(myToggle.GetCurrent(), transform.position.y, transform.position.z);
}
}
Your new Toggle function in C#:
ToggleBackForth.cs
using UnityEngine;
using System.Collections;
public class ToggleBackForth
{
public bool done;
public float current;
public float stopat;
public float buffer;
public float toggleTime;
//default constructor if using this you need to manually start the timer using StartTimer()
public ToggleBackForth()
{
done = false;
current = -6.6f;
stopat = 0.57f;
buffer = current;
toggleTime = 5.0f;
}
public ToggleBackForth(float start, float end, float duration)
{
done = false;
current = start;
stopat = end ;
buffer = current;
toggleTime = duration;
}
public IEnumerator StartTimer()
{
while(!done)
{
buffer = current;
current = stopat;
stopat = buffer;
yield return new WaitForSeconds(toggleTime);
}
}
public void StopTimer()
{
done = true;
}
public float GetCurrent()
{
return current;
}
}