返回一个标准化矢量,它表示世界空间中变换的蓝轴。
以下示例说明如何操作 GameObject 在世界空间中变换的 Z 轴(蓝轴)上的位置。与 Vector3.forward 不同,Transform.forward 在移动 GameObject 的同时,还考虑其旋转。
旋转游戏对象时,表示游戏对象的 Z 轴的蓝色箭头也会改变方向。Transform.forward 沿蓝色箭头所在的轴 (Z) 移动游戏对象。
要在沿 Z 轴移动 GameObject 时忽略旋转,请参阅 Vector3.forward。
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); } } }
另一个示例:
using UnityEngine;
// Computes the angle between the target transform and this object
public class Example : MonoBehaviour { public float angleBetween = 0.0f; public Transform target;
void Update() { Vector3 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
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.