how do i make an object loop

I am trying to make both cubes and barrels move but once they hit a certain point i need them to go back to where they started if you know a javascript for 2d that works that would be a life saver

It is just as easy to animate them. If you set the animation to loop, once it hits the end it will start back over at the beginning. If you don't want it to loop then add an animation event at the end of the animation.

here's a code example if you want to code it:

var cube : GameObject;

var barrel : GameObject;

var xStart : int = 0;

var xEnd : int = 10;

var xMove : int = 1;

function Update() {

if (cube.transform.position.x < xEnd) { cube.transform.Translate(0, 0, xMove * Time.deltaTime);//time.deltaTime move it every second no matter what the frame rate//

} else { cube.transform.position.x = xStart; }

if (barrel .transform.position.x < xEnd) {
   barrel .transform.Translate(0, 0, xMove * Time.deltaTime);
 } else {
    barrel .transform.position.x = xStart;
 }  

}

Assuming you already have your barrels and cubes moving using rigidbody physics, this is how you'd put an object back to its starting position if it reaches a certain position:

  • Make a GameObject with a trigger collider at the end point. Make sure it's large enough that your objects will definitely hit it.

  • Create a new Tag. Call it "RestartPoint". Tag the GameObject you just made with this tag.

  • Place this script on your moving objects:

    private var startPos;
    private var startRot;
    
    function Start() {
        startPos = transform.position;
        startRot = transform.rotation;
    }
    
    function OnTriggerEnter(other : Collider) {
        if (other.tag == "RestartPoint") {
            Restart();
        }
    }
    
    function Restart() {
        transform.position = startPos;
        transform.rotation = startRot;
        rigidbody.velocity = Vector3.zero;
        rigidbody.angularVelocity = Vector3.zero;
    }
    
    

Your objects should now return to their starting position if they enter any trigger collider which is tagged as "RestartPoint".

Hope this helps!