版本: Unity 6 (6000.0)
语言英语
  • C#

Transform.right

建议更改

成功!

感谢您帮助我们改进 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们确实会阅读来自用户的每项建议更改,并在适用的情况下进行更新。

关闭

提交失败

由于某种原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们改进 Unity 文档的质量。

关闭

取消

切换到手册
public Vector3 right;

描述

变换在世界空间中的红色轴。

在世界空间中,沿变换的 X 轴(红色轴)操作游戏对象的方位。与 Vector3.right 不同,Transform.right 在移动游戏对象的同时也会考虑其旋转。

当游戏对象旋转时,表示游戏对象 X 轴的红色箭头也会改变方向。Transform.right 将游戏对象沿着红色箭头的轴(X)移动。

要忽略旋转,在 X 轴上移动游戏对象,请参阅 Vector3.right

//Attach this script to a GameObject with a Rigidbody2D component. Use the left and right arrow keys to see the transform in action.
//Use the up and down keys to change the rotation, and see how using Transform.right differs from using Vector3.right

using UnityEngine;

public class Example : MonoBehaviour { Rigidbody2D m_Rigidbody; float m_Speed;

void Start() { //Fetch the Rigidbody component you attach from your GameObject m_Rigidbody = GetComponent<Rigidbody2D>(); //Set the speed of the GameObject m_Speed = 10.0f; }

void Update() { if (Input.GetKey(KeyCode.RightArrow)) { //Move the Rigidbody to the right constantly at speed you define (the red arrow axis in Scene view) m_Rigidbody.velocity = transform.right * m_Speed; }

if (Input.GetKey(KeyCode.LeftArrow)) { //Move the Rigidbody to the left constantly at the speed you define (the red arrow axis in Scene view) m_Rigidbody.velocity = -transform.right * m_Speed; }

if (Input.GetKey(KeyCode.UpArrow)) { //rotate the sprite about the Z axis in the positive direction transform.Rotate(new Vector3(0, 0, 1) * Time.deltaTime * m_Speed, Space.World); }

if (Input.GetKey(KeyCode.DownArrow)) { //rotate the sprite about the Z axis in the negative direction transform.Rotate(new Vector3(0, 0, -1) * Time.deltaTime * m_Speed, Space.World); } } }