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);
}
}
}
If you are adding this script to each of the objects, do the following. This only requires one script, and it is added to each of the cup
objects.
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
public class rotateCups : MonoBehaviour
{
private float lerp = 0f, speed = 5f;
static List<Vector3> listOfCupPositions, shuffleList;
Vector3 theNewPos, startPos;
readonly int shuffleSpeed = 100;
int shuffle = 0;
void Start()
{
if (null == listOfCupPositions)
{
// These lists are global to each cup
listOfCupPositions = new List<Vector3>();
shuffleList = new List<Vector3>();
}
theNewPos = startPos = transform.position;
listOfCupPositions.Add(theNewPos); // Add this cup to the main list
}
void Update()
{
if (startPos != theNewPos)
{
lerp += Time.deltaTime * speed;
lerp = Mathf.Clamp(lerp, 0f, 1f); // keep lerp between the values 0..1
transform.position = Vector3.Lerp(startPos, theNewPos, lerp);
if (lerp >= 1f)
{
startPos = theNewPos;
lerp = 0f;
}
}
}
void LateUpdate()
{
if (--shuffle <= 0)
{
// Shuffle the cups
shuffle = shuffleSpeed;
if (0 == shuffleList.Count)
shuffleList = listOfCupPositions.ToList(); // refresh shuffle positions
// Loop until we get a position this cup isn't,
// or unless there's only one spot left in shuffle list,
// use it (ie don't move this cup this round)
int index;
do
{
index = Random.Range(0, shuffleList.Count);
} while (startPos == shuffleList[index] && shuffleList.Count > 1);
// give this cup a new position
theNewPos = shuffleList[index];
shuffleList.RemoveAt(index); // remove position from shuffle list so it isn't duplicated to another cup
}
}
}
This is a fun little thing to watch in action. To test this, I added a cube, capsule and a sphere, moved them to 3 separate spots, added the script to each, and then with the camera selected, did a ctrl+shift+F to give it the same view point as my editor window, (save the scene first just in case), then run it.