Select your preferred scripting language. All code snippets will be displayed in this language.
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseThe blue axis of the transform in world space.
Manipulate a GameObject’s position on the Z axis (blue axis) of the transform in world space. Unlike Vector3.forward, Transform.forward moves the GameObject while also considering its rotation.
When a GameObject is rotated, the blue arrow representing the Z axis of the GameObject also changes direction. Transform.forward moves the GameObject in the blue arrow’s axis (Z).
For moving the GameObject on the Z axis while ignoring rotation, see Vector3.forward.
var thrust: float; var rb: Rigidbody;
function Start() { rb = GetComponent.<Rigidbody>(); }
function Update () { rb.AddForce(transform.forward * thrust); }
using UnityEngine;
public class Example : MonoBehaviour { Rigidbody m_Rigidbody; float m_Speed;
void Start() { //Fetch the Rigidbody component you attach from your GameObject m_Rigidbody = GetComponent<Rigidbody>(); //Set the speed of the GameObject m_Speed = 10.0f; }
void Update() { if (Input.GetKey(KeyCode.UpArrow)) { //Move the Rigidbody forwards constantly at speed you define (the blue arrow axis in Scene view) m_Rigidbody.velocity = transform.forward * m_Speed; }
if (Input.GetKey(KeyCode.DownArrow)) { //Move the Rigidbody backwards constantly at the speed you define (the blue arrow axis in Scene view) m_Rigidbody.velocity = -transform.forward * m_Speed; }
if (Input.GetKey(KeyCode.RightArrow)) { //Rotate the sprite about the Y axis in the positive direction transform.Rotate(new Vector3(0, 1, 0) * Time.deltaTime * m_Speed, Space.World); }
if (Input.GetKey(KeyCode.LeftArrow)) { //Rotate the sprite about the Y axis in the negative direction transform.Rotate(new Vector3(0, -1, 0) * Time.deltaTime * m_Speed, Space.World); } } }
Another example:
// Computes the angle between the target transform and this object var angleBetween = 0.0; var target : Transform; function Update () { var targetDir = target.position - transform.position; angleBetween = Vector3.Angle (transform.forward, targetDir); }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information