- Home /
Is there a way to have an object gradually move to one spot, move back, and then stop?
Short version: Is there a way to have an object gradually move to one spot, move back, and then stop?
Long version: I'm trying to make an enemy for a game that moves along the ground and then hops up every two seconds. I've tried to use the Mathf.PingPong function, but I could not find a way to stop it once it gets going. Further, if I put the command in the Start() function, the object teleports to the destinations instead of gradually moving from one spot to the other.
It would be great if there was a Transform command that gradually moves an object to a designated spot, but my searches here and on the web have not turned up anything. If I had that, I could use "while" and "yield WaitForSeconds" to accomplish my AI movement. But the best I can do is have the objects teleport to a certain spot or have an object continuously move up and down.
Any ideas?
Here is my code for those who are curious:
#pragma strict
//Stores the position the enemy spawns at
var initPos : Vector3;
var hopDirection : Vector2;
var hopSpeed : float;
function Start () {
initPos = transform.position;
}
function Update() {
transform.Translate(Time.deltaTime * -5, 0, 0);
}
function TumbleHop() {
transform.position.y = initPos.y + Mathf.PingPong(Time.time * hopSpeed, hopDirection.y);
transform.position.x = initPos.x + Mathf.PingPong(Time.time * hopSpeed, hopDirection.x);
}