I thought I’d write up a script for you. I didn’t think it’d be super complex, but it uses my mapping function which is so amazing for things like this.
var startPos:Vector3 = Vector3.zero; //Set these for certain.
var endPos:Vector3 = Vector3.zero;
var moveDuration:float = 5; //(in seconds)
private var moveDurationValue:float = 0;
private var moveDirection:boolean = true;
var delayDuration:float = 5;
private var delayDurationValue:float = 0;
private var delaying:boolean = false;
function Update()
{
if(delaying)
{
delayDurationValue += Time.deltaTime;
if(delayDurationValue >= delayDuration)
{
delaying=false;
moveDirection = !moveDirection;
delayDurationValue = 0;
}
}else{
if(moveDirection)
{
moveDurationValue += Time.deltaTime;
if(moveDurationValue >= moveDuration)
{
delaying = true;
}
}else{
moveDurationValue -= Time.deltaTime;
if(moveDurationValue <= 0)
{
delaying = true;
}
}
transform.position.x = Map(moveDurationValue, 0, moveDuration, startPos.x, endPos.x);
transform.position.y = Map(moveDurationValue, 0, moveDuration, startPos.y, endPos.y);
transform.position.z = Map(moveDurationValue, 0, moveDuration, startPos.z, endPos.z);
}
}
function Map(value:float, istart:float, istop:float, ostart:float, ostop:float)
{
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}
Simple as attaching the script to the gameobject you want to move, then set the start pos and end pos, and finally decide on your movement time and your delay time. System works flawlessly. (I already tested it with a basic cube in Unity 3.1)
Hope this helps you. It may seem a little complex, but it’s extremely scalable.
Oh, and since your doing it in 2d, here is a vector2 version:
var startPos:Vector2 = Vector2.zero; //Set these for certain.
var endPos:Vector2 = Vector2.zero;
var moveDuration:float = 5; //(in seconds)
private var moveDurationValue:float = 0;
private var moveDirection:boolean = true;
var delayDuration:float = 5;
private var delayDurationValue:float = 0;
private var delaying:boolean = false;
function Update()
{
if(delaying)
{
delayDurationValue += Time.deltaTime;
if(delayDurationValue >= delayDuration)
{
delaying=false;
moveDirection = !moveDirection;
delayDurationValue = 0;
}
}else{
if(moveDirection)
{
moveDurationValue += Time.deltaTime;
if(moveDurationValue >= moveDuration)
{
delaying = true;
}
}else{
moveDurationValue -= Time.deltaTime;
if(moveDurationValue <= 0)
{
delaying = true;
}
}
transform.position.x = Map(moveDurationValue, 0, moveDuration, startPos.x, endPos.x);
transform.position.y = Map(moveDurationValue, 0, moveDuration, startPos.y, endPos.y);
}
}
function Map(value:float, istart:float, istop:float, ostart:float, ostop:float)
{
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}
You can look through the code to under stand it, but it’s scripted in a way that you don’t need to even look at the code.
Yay for easy to use crap!!!