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: http://i249.photobucket.com/albums/gg240/OldNewby4507/shell-game-animated.gif
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);
}
}
}