Transform.up

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

Transform.up

public Vector3 up;

Описание

Зеленая ось преобразования в мировом пространстве.

Управление положением GameObject по оси Y (зеленая ось) преобразования в мировом пространстве. В отличие от Vector3.up, Transform.up перемещает GameObject с учетом его поворота.

При повороте игрового объекта зеленая стрелка, обозначающая ось Y игрового объекта, также меняет направление. Transform.up перемещает GameObject по оси зеленой стрелки (Y).

Информацию о перемещении GameObject по оси Y без учета поворота см. в Vector3.up.

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.UpArrow))
        {
            //Move the Rigidbody upwards constantly at speed you define (the green arrow axis in Scene view)
            m_Rigidbody.velocity = transform.up * m_Speed;
        }

        if (Input.GetKey(KeyCode.DownArrow))
        {
            //Move the Rigidbody downwards constantly at the speed you define (the green arrow axis in Scene view)
            m_Rigidbody.velocity = -transform.up * m_Speed;
        }

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            //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.RightArrow))
        {
            //rotate the sprite about the Z axis in the negative direction
            transform.Rotate(new Vector3(0, 0, -1) * Time.deltaTime * m_Speed, Space.World);
        }
    }
}

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