Version: 2020.3
public Vector3 right ;

描述

世界空间中变换的红轴。

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

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

要在沿 X 轴移动 GameObject 时忽略旋转,请参阅 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); } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961