这是一个 8 方向移动枚举。
MoveDirection 提供了一种在移动状态之间切换的方式。您必须将这些状态分配给操作,例如,处于 Up 状态时,通过向上矢量移动游戏对象。
与必须始终包含大量矢量和计算相比,具有此类状态更容易识别。相反,您只需在一个部分中定义希望状态执行的操作,并在需要时切换到相应状态即可。
//This is a full example of how a GameObject changes direction using MoveDirection states //Assign this script to a visible GameObject (with a Rigidbody attached) to see it in action
using UnityEngine; using UnityEngine.EventSystems;
public class Example : MonoBehaviour { Vector3 m_StartPosition, m_StartForce; Rigidbody m_Rigidbody; //Use Enum for easy switching between direction states MoveDirection m_MoveDirection;
//Use these Vectors for moving Rigidbody components Vector3 m_ResetVector; Vector3 m_UpVector; Vector3 m_RightVector; const float speed = 5.0f;
void Start() { //You get the Rigidbody component attached to the GameObject m_Rigidbody = GetComponent<Rigidbody>(); //This starts with the Rigidbody not moving in any direction at all m_MoveDirection = MoveDirection.None;
//These are the GameObject’s starting position and Rigidbody position m_StartPosition = transform.position; m_StartForce = m_Rigidbody.transform.position;
//This Vector is set to 1 in the y axis (for moving upwards) m_UpVector = Vector3.up; //This Vector is set to 1 in the x axis (for moving in the right direction) m_RightVector = Vector3.right; //This Vector is zeroed out for when the Rigidbody should not move m_ResetVector = Vector3.zero; }
void Update() { //This switches the direction depending on button presses switch (m_MoveDirection) { //The starting state which resets the object case MoveDirection.None: //Reset to the starting position of the GameObject and Rigidbody transform.position = m_StartPosition; m_Rigidbody.transform.position = m_StartForce; //This resets the velocity of the Rigidbody m_Rigidbody.velocity = m_ResetVector; break;
//This is for moving in an upwards direction case MoveDirection.Up: //Change the velocity so that the Rigidbody travels upwards m_Rigidbody.velocity = m_UpVector * speed; break;
//This is for moving left case MoveDirection.Left: //This moves the Rigidbody to the left (minus right Vector) m_Rigidbody.velocity = -m_RightVector * speed; break;
//This is for moving right case MoveDirection.Right: //This moves the Rigidbody to the right m_Rigidbody.velocity = m_RightVector * speed; break;
//This is for moving down case MoveDirection.Down: //This moves the Rigidbody down m_Rigidbody.velocity = -m_UpVector * speed; break; } }
void OnGUI() { //Press the reset Button to switch to no mode if (GUI.Button(new Rect(100, 0, 150, 30), "Reset")) { //Switch to start/reset case m_MoveDirection = MoveDirection.None; }
//Press the Left button to switch the Rigidbody direction to the left if (GUI.Button(new Rect(100, 30, 150, 30), "Move Left")) { //Switch to the left direction m_MoveDirection = MoveDirection.Left; }
//Press the Up button to switch the Rigidbody direction to upwards if (GUI.Button(new Rect(100, 60, 150, 30), "Move Up")) { //Switch to Up Direction m_MoveDirection = MoveDirection.Up; }
//Press the Down button to switch the direction to down if (GUI.Button(new Rect(100, 90, 150, 30), "Move Down")) { //Switch to Down Direction m_MoveDirection = MoveDirection.Down; }
//Press the right button to switch to the right direction if (GUI.Button(new Rect(100, 120, 150, 30), "Move Right")) { //Switch to Right Direction m_MoveDirection = MoveDirection.Right; } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.