Описание
Красная ось преобразования в мировом пространстве.
Управление положением GameObject по оси X (красная ось) преобразования в мировом пространстве. В отличие от Vector3.right, Transform.right перемещает GameObject с учетом его поворота.
Когда игровой объект вращается, красная стрелка, обозначающая ось X игрового объекта, также меняет направление. Transform.right перемещает GameObject по оси красной стрелки (X).
Информацию о перемещении GameObject по оси X без учета поворота см. в разделе Vector3.right.
//Прикрепите этот скрипт к GameObject с компонентом Rigidbody2D. Используйте клавиши со стрелками влево и вправо, чтобы увидеть преобразование в действии.
//Используйте клавиши вверх и вниз, чтобы изменить поворот, и посмотрите, чем использование Transform.right отличается от использования 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);
}
}
}