How can I transform 3 objects to swap positions, but make sure they aren't moving to a position an object is already going to?

I have applied a code to 3 objects. So far this code switches three cups around so that they swap places. The code gets the start position of the cup, and then the end position of each cup. All 3 end positions are actually the start positions of the other cups, since I want them to swap to the same exact positions as the other cups. The update function makes it so that if the cup’s end position (that is chosen at random) DOES NOT equal it’s start position, it will move to one of the other 2 spots that were not it’s start position, randomly.

I am having trouble with figuring out a couple of things and have tried a few things, but I don’t think I’m getting the code right. I want to:

Make the cups randomly move to their new spot, but not move a cup to a spot that another cup is already moving to. (Currently when the animation plays, 2 cups potentially move to the same spot of the 3 spots).

I am just not sure what direction to go in. I also would like this animation to keep playing and for the cups to keep randomly swapping their spots. It would be helpful to get a point in the direction of figuring that out as well.

This is what I want the final outcome to resemble:

Thanks in advance.

using UnityEngine;
    using System.Collections;
    using UnityEngine;
    using System.Collections;

    public class NewBehaviourScript : MonoBehaviour
        private Vector3 startPos;
        private float lerp = 0, duration = 1;
        public Vector3[] endPos = new Vector3[3];
        int index;
        Vector3 theNewPos;

        void Start ()
            startPos = transform.position;
            endPos [0] = GameObject.Find ("object1").transform.position;
            endPos [1] = GameObject.Find ("object2").transform.position;
            endPos [2] = GameObject.Find ("object3").transform.position;
            index = Random.Range (0, endPos.Length);
            theNewPos = endPos[index];


        void Update() {

            if (theNewPos != startPos) { 
                lerp += Time.deltaTime / duration;
                transform.position = Vector3.Lerp (startPos, theNewPos, lerp);



A good question!

First thing I would do is put the target-selecting logic you’re talking about in a manager class instead of on scripts for the individual objects. Think of it as centralizing control. Suddenly you have access to all three objects within the same context (the same script) and can leverage this to work with your scene information more efficiently. This will be a common theme in your programming experience. :slight_smile:

Having a manager that helps you perform tasks on related objects is nearly always a good strategy for organization. In this case, your ShellManager could actually replace your Shell script altogether, or you could put only the target-picking logic in the manager.

With all three shells in the same scope, it becomes very simple to assign each one a guaranteed-unique target. Have your shells in a collection. When it’s time to pick new targets, for each shell, pick a random element of the collection. If it’s the same shell you’re trying to assign a target for, or if you’ve already picked that element, pick another. Assign the randomized, guaranteed-unique shell you’ve just found as the target position.

Getting consistent, quality animations is too big a topic for this UA post, but feel free to make another when the time comes. Learning to repeat said animations is not really a valid UA question because that information is easily available.