我已将代码应用于3个对象。到目前为止,该代码将三杯切换到周围,以便交换位置。代码获得杯子的起始位置,然后是每个杯子的末端位置。实际上,所有3个端位置都是其他杯子的开始位置,因为我希望它们与其他杯子的确切位置交换。更新功能使得它使得杯子的结束位置(随机选择)不等,它的开始位置将移至其他2个不是它的启动位置之一,随机。
。我在找出几件事并尝试了几件事方面遇到了麻烦,但是我认为我的代码不正确。我想:
将杯子随机移至新位置,但不要将杯子移至另一个杯子已经移至的位置。(目前,当动画播放时,2杯有可能移至3个景点的同一地点)。
我只是不确定要走什么方向。我还希望这个动画继续演奏,并希望杯子随机交换其位置。要弄清楚这一点的方向有所帮助。
这是我想要的最终结果类似于:http://i249.photobucket.com/albums/gg240/oldnewby4507/shell-game-animated.gif
预先感谢。
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);
}
}
}
如果将此脚本添加到每个对象中,请执行以下操作。这只需要一个脚本,然后将其添加到cup
对象中。
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
}
}
}
这是一件有趣的小事。为了测试这一点,我添加了一个立方体,胶囊和一个球,将它们移到3个单独的斑点,将脚本添加到每个斑点,然后使用相机选择了一个Ctrl Shift F,以使其与我的观点相同编辑窗口(首先保存场景,以防万一),然后运行。
我认为您必须为杯子制作这样的代码:
using UnityEngine;
public class Cup : MonoBehaviour
{
public Vector3 startPos;
private float lerp = 0, duration = 1;
Vector3 theNewPos;
void Awake()
{
startPos = transform.position;
theNewPos = transform.position;
}
void Start()
{
}
void Update()
{
float thresholdSqr = 0.01f;
if ((theNewPos - transform.position).sqrMagnitude > thresholdSqr)
{
lerp += Time.deltaTime / duration;
transform.position = Vector3.Lerp(startPos, theNewPos, lerp);
}
else
{
transform.position = theNewPos;
startPos = transform.position;
}
}
public void MoveToSpot(Vector3 pos)
{
theNewPos = pos;
lerp = 0f;
}
}
,然后使用这样的代码进行全局游戏对象振动器:
using UnityEngine;
using System.Linq;
public class Shaker : MonoBehaviour
{
Cup[] cups;
Vector3[] spots;
void Start()
{
cups = GameObject.FindObjectsOfType<Cup>();
spots = cups.Select(c => c.startPos).ToArray();
}
void Update()
{
}
void Shake()
{
int[] newIndices = Enumerable.Repeat(-1, cups.Length).ToArray();
for (int i = 0; i < cups.Length; i++) //there could be another random sort algorithm, for example to make that every cup would move to another spot.
{
int free = cups.Length - i;
int j = Random.Range(0, free);
for (int k = j; k < cups.Length; k++)
{
if (newIndices[k] == -1)
{
newIndices[k] = i;
break;
}
}
}
for (int i = 0; i < cups.Length; i++)
{
cups[i].MoveToSpot(spots[newIndices[i]]);
}
}
}