Space

Unity · Версия 2020.3 LTS · Scripting

Space

Описание

Координатное пространство, в котором нужно работать.

Используйте Space.world для преобразования игрового объекта с использованием мировых координат Unity, игнорируя состояние вращения игрового объекта. Используйте Space.self для преобразования GameObject с использованием его собственных координат и учета его поворотов.

Смотрите так же: Transform.

//Attach this script to a GameObject
//This example shows the difference between using Space.world and Space.self.
//Enable or disable the checkbox in the Inspector before starting (depending on if you want world or self)
//Press play to see the GameObject rotating appropriately. Press space to switch between world and self.

using UnityEngine;

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

    void Start()
    {
        //Set the speed of the rotation
        m_Speed = 20.0f;
        //Start off in World.Space
        m_WorldSpace = true;
        //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());
        }
    }
}

Свойства

World Применяет преобразование относительно мировой системы координат.
Self Применяет преобразование относительно локальной системы координат.

Обновлено 17 ноября 2026 г.