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

Space.Self

建议修改

成功!

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

关闭

提交失败

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

关闭

取消

描述

相对于其父级而言的 GameObject 的局部坐标系,包括方向。

使用 Space.Self 相对于其 Transform.localPosition 和其自身局部轴来变换 GameObject,同时考虑其方向。例如, Transform.Translate(Vector3.forward, Space.Self) 会在对象的 z 轴上(这可能与场景的 z 轴不同,具体取决于对象的朝向)添加一个单位到对象的 Transform.localPosition

要相对于其 Transform.position 并在场景轴上变换 GameObject,请使用 Space.World

//Attach this script to a GameObject.
//This example demonstrates the difference between Space.World and Space.Self in rotation.
//The m_WorldSpace field will be automatically exposed as a checkbox in the Inspector window labelled World Space. Enable or disable the checkbox in the Inspector to start in world or self respectively.
//Press play to see the GameObject rotating appropriately. Press space or toggle the World Space checkbox to switch between World and Self.

using UnityEngine;

public class Example : MonoBehaviour { float m_Speed; public bool m_WorldSpace;

void Start() { //Set the speed of the rotation m_Speed = 20.0f; //Rotate the GameObject a little at the start to show the difference between Space and Local transform.Rotate(60, 0, 60); }

void Update() { //Rotate the GameObject in World Space if in the m_WorldSpace state if (m_WorldSpace) transform.Rotate(Vector3.up * m_Speed * Time.deltaTime, Space.World); //Otherwise, rotate the GameObject in local space else transform.Rotate(Vector3.up * m_Speed * Time.deltaTime, Space.Self);

//Press the Space button to switch between world and local space states if (Input.GetKeyDown(KeyCode.Space)) { //Make the current state switch to the other state m_WorldSpace = !m_WorldSpace; //Output the Current state to the console Debug.Log("World Space : " + m_WorldSpace.ToString()); } } }