(3D物体运动)开关控制3D朝着六个方向移动一定的距离
using UnityEngine; using System.Collections; using UnityEngine.UI; public class DirectionMove : MonoBehaviour { public GameObject WestMoveTog;//西移 public GameObject EastMoveTog;//东移 public GameObject UpMoveTog;//上移 public GameObject DownMoveTog;//下移 public GameObject ForwardTog;//前进 public GameObject BackTog;//后退 bool flag1;//上行判断 bool flag2;//下行判断 bool flag3;//东移判断 bool flag4;//西移判断 bool flag5;//前进判断 bool flag6;//后退判断 public GameObject MoveJiaZi;//移动架子 Vector3 GoalPos1;//上行目标位置 Vector3 GoalPos2;//下行目标位置 Vector3 GoalPos3;//东移目标位置 Vector3 GoalPos4;//西移目标位置 Vector3 GoalPos5;//前进目标位置 Vector3 GoalPos6;//后退目标位置 void Start () { EventTriggerListener.Get(WestMoveTog).onClick = DirctionControl; EventTriggerListener.Get(EastMoveTog).onClick = DirctionControl; EventTriggerListener.Get(UpMoveTog).onClick = DirctionControl; EventTriggerListener.Get(DownMoveTog).onClick = DirctionControl; EventTriggerListener.Get(ForwardTog).onClick = DirctionControl; EventTriggerListener.Get(BackTog).onClick = DirctionControl; } void DirctionControl(GameObject game) { if (game == UpMoveTog) { //上行 GoalPos1 = new Vector3(MoveJiaZi.transform.position.x, MoveJiaZi.transform.position.y + 2f, MoveJiaZi.transform.position.z); flag1 = true; } else if (game == DownMoveTog) { //下行 GoalPos2 = new Vector3(MoveJiaZi.transform.position.x, MoveJiaZi.transform.position.y - 2f, MoveJiaZi.transform.position.z); flag2 = true; } else if (game == EastMoveTog) { //东移 GoalPos3 = new Vector3(MoveJiaZi.transform.position.x, MoveJiaZi.transform.position.y, MoveJiaZi.transform.position.z +4.5f); flag3 = true; } else if (game == WestMoveTog) { //西移 GoalPos4 = new Vector3(MoveJiaZi.transform.position.x, MoveJiaZi.transform.position.y, MoveJiaZi.transform.position.z -4.5f); flag4 = true; } else if (game == ForwardTog) { //前进 GoalPos5 = new Vector3(MoveJiaZi.transform.position.x - 0.3f, MoveJiaZi.transform.position.y, MoveJiaZi.transform.position.z); flag5 = true; } else if (game == BackTog) { //后退 GoalPos6 = new Vector3(MoveJiaZi.transform.position.x + 0.3f, MoveJiaZi.transform.position.y, MoveJiaZi.transform.position.z); flag6 = true; } } void Update () { if (flag1) { MoveJiaZi.transform.position = Vector3.MoveTowards(MoveJiaZi.transform.position, GoalPos1, 0.01f); Debug.Log("上行后物体的位置" + MoveJiaZi.transform.position); if (MoveJiaZi.transform.position==GoalPos1) { flag1 = false; } } else if (flag2) { MoveJiaZi.transform.position = Vector3.MoveTowards(MoveJiaZi.transform.position, GoalPos2, 0.01f); Debug.Log("下行后物体的位置" + MoveJiaZi.transform.position); if (MoveJiaZi.transform.position == GoalPos2) { flag2 = false; } } else if (flag3) { MoveJiaZi.transform.position = Vector3.MoveTowards(MoveJiaZi.transform.position, GoalPos3, 0.01f); Debug.Log("东移后物体的位置" + MoveJiaZi.transform.position); if (MoveJiaZi.transform.position == GoalPos3) { flag3 = false; } } else if (flag4) { MoveJiaZi.transform.position = Vector3.MoveTowards(MoveJiaZi.transform.position, GoalPos4, 0.01f); Debug.Log("西移后物体的位置" + MoveJiaZi.transform.position); if (MoveJiaZi.transform.position == GoalPos4) { flag4 = false; } } else if (flag5) { MoveJiaZi.transform.position = Vector3.MoveTowards(MoveJiaZi.transform.position, GoalPos5, 0.01f); Debug.Log("前进后物体的位置" + MoveJiaZi.transform.position); if (MoveJiaZi.transform.position == GoalPos5) { flag5 = false; } } else if (flag6) { MoveJiaZi.transform.position = Vector3.MoveTowards(MoveJiaZi.transform.position, GoalPos6, 0.01f); Debug.Log("后退后物体的位置" + MoveJiaZi.transform.position); if (MoveJiaZi.transform.position == GoalPos6) { flag6 = false; } } } }