版本:Unity 6 (6000.0)
语言英语
  • C#

Quaternion.SetFromToRotation

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实阅读了用户提出的每一个建议更改,并在适用的情况下进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

切换到手册

声明

public void SetFromToRotation(Vector3 fromDirection, Vector3 toDirection);

描述

创建一个从 fromDirection 旋转到 toDirection 的旋转。

使用此方法创建从第一个向量(fromDirection)开始并旋转到第二个向量(toDirection)的旋转。这些向量必须在脚本中设置。

//This example shows how the rotation works between two GameObjects. Attach this to a GameObject.
//Make sure to assign the GameObject you would like your GameObject to rotate towards in the Inspector

using UnityEngine;

public class SetFromToRotationExample : MonoBehaviour { //This is the Transform of the second GameObject public Transform m_NextPoint; Quaternion m_MyQuaternion; float m_Speed = 1.0f;

void Start() { m_MyQuaternion = new Quaternion(); }

void Update() { //Set the Quaternion rotation from the GameObject's position to the next GameObject's position m_MyQuaternion.SetFromToRotation(transform.position, m_NextPoint.position); //Move the GameObject towards the second GameObject transform.position = Vector3.Lerp(transform.position, m_NextPoint.position, m_Speed * Time.deltaTime); //Rotate the GameObject towards the second GameObject transform.rotation = m_MyQuaternion * transform.rotation; } }
//In this example your GameObject rotates towards the position of the mouse

using UnityEngine;

public class Example2 : MonoBehaviour { Quaternion m_MyQuaternion; float m_Speed = 1.0f; Vector3 m_MousePosition;

void Start() { m_MyQuaternion = new Quaternion(); }

void Update() { //Fetch the mouse's position m_MousePosition = Input.mousePosition; //Fix how far into the Scene the mouse should be m_MousePosition.z = 50.0f; //Transform the mouse position into world space m_MousePosition = Camera.main.ScreenToWorldPoint(m_MousePosition);

//Set the Quaternion rotation from the GameObject's position to the mouse position m_MyQuaternion.SetFromToRotation(transform.position, m_MousePosition); //Move the GameObject towards the mouse position transform.position = Vector3.Lerp(transform.position, m_MousePosition, m_Speed * Time.deltaTime); //Rotate the GameObject towards the mouse position transform.rotation = m_MyQuaternion * transform.rotation; } }