Unity LookAt,而是将整个 body 旋转到 3d space 方向
Unity LookAt, but instead rotate entire body towards direction in 3d space
浪费了很多时间试图弄清楚轮换和寻找答案,但无法找到适合我的问题的任何东西。我需要将整个游戏对象旋转到特定方向而不是沿 y 轴旋转:
1) 在 Quaternion.LookRotation 内或由 Atan2 给定方向时 object 当前如何旋转。
2,3) 应该如何旋转的例子。红色 ot 表示旋转发生的枢轴点
没有太多代码可以显示,因为除了旋转的 gameObject 转换和旋转 gameObject 的方向之外没有太多内容。
根据要求
[System.Serializable]
public class ToRotate
{
//Object which will be rotated by the angle
public GameObject gameObject;
//Object last known position of this object. The object is rotated towards it's last global position
private Vector3 lastPosition;
//Initializes starting world position values to avoid a strange jump at the start.
public void Init()
{
if (gameObject == null)
return;
lastPosition = gameObject.transform.position;
}
//Method which updates the rotation
public void UpdateRotation()
{
//In order to avoid errors when object given is null.
if (gameObject == null)
return;
//If the objects didn't move last frame, no point in recalculating and having a direction of 0,0,0
if (lastPosition == gameObject.transform.position)
return;
//direction the rotation must face
Vector3 direction = (lastPosition - gameObject.transform.position).normalized;
/* Code that modifies the rotation angle is written here */
//y angle
float angle = Mathf.Rad2Deg * Mathf.Atan2(direction.x, direction.z);
Quaternion rotation = Quaternion.Euler(0, angle, 0);
gameObject.transform.rotation = rotation;
lastPosition = gameObject.transform.position;
}
}
由于希望物体的局部向下指向一个计算好的方向,同时尽量保持物体的局部右不变,我将使用 Vector3.Cross
to find the cross product of that down and right to determine the direction the object's local forward should face, then use Quaternion.LookRotation 来获得相应的旋转:
//Method which updates the rotation
public void UpdateRotation()
{
//In order to avoid errors when object given is null.
if (gameObject == null)
return;
//If the objects didn't move last frame, no point in recalculating and having a direction of 0,0,0
if (lastPosition == transform.position)
return;
//direction object's local down should face
Vector3 downDir = (lastPosition - transform.position).normalized;
// direction object's local right should face
Vector3 rightDir = transform.right;
// direction object's local forward should face
Vector3 forwardDir = Vector3.Cross(downDir, rightDir);
transform.rotation = Quaternion.LookRotation(forwardDir, -downDir);
lastPosition = transform.position;
}
浪费了很多时间试图弄清楚轮换和寻找答案,但无法找到适合我的问题的任何东西。我需要将整个游戏对象旋转到特定方向而不是沿 y 轴旋转:
1) 在 Quaternion.LookRotation 内或由 Atan2 给定方向时 object 当前如何旋转。 2,3) 应该如何旋转的例子。红色 ot 表示旋转发生的枢轴点
没有太多代码可以显示,因为除了旋转的 gameObject 转换和旋转 gameObject 的方向之外没有太多内容。
根据要求
[System.Serializable]
public class ToRotate
{
//Object which will be rotated by the angle
public GameObject gameObject;
//Object last known position of this object. The object is rotated towards it's last global position
private Vector3 lastPosition;
//Initializes starting world position values to avoid a strange jump at the start.
public void Init()
{
if (gameObject == null)
return;
lastPosition = gameObject.transform.position;
}
//Method which updates the rotation
public void UpdateRotation()
{
//In order to avoid errors when object given is null.
if (gameObject == null)
return;
//If the objects didn't move last frame, no point in recalculating and having a direction of 0,0,0
if (lastPosition == gameObject.transform.position)
return;
//direction the rotation must face
Vector3 direction = (lastPosition - gameObject.transform.position).normalized;
/* Code that modifies the rotation angle is written here */
//y angle
float angle = Mathf.Rad2Deg * Mathf.Atan2(direction.x, direction.z);
Quaternion rotation = Quaternion.Euler(0, angle, 0);
gameObject.transform.rotation = rotation;
lastPosition = gameObject.transform.position;
}
}
由于希望物体的局部向下指向一个计算好的方向,同时尽量保持物体的局部右不变,我将使用 Vector3.Cross
to find the cross product of that down and right to determine the direction the object's local forward should face, then use Quaternion.LookRotation 来获得相应的旋转:
//Method which updates the rotation
public void UpdateRotation()
{
//In order to avoid errors when object given is null.
if (gameObject == null)
return;
//If the objects didn't move last frame, no point in recalculating and having a direction of 0,0,0
if (lastPosition == transform.position)
return;
//direction object's local down should face
Vector3 downDir = (lastPosition - transform.position).normalized;
// direction object's local right should face
Vector3 rightDir = transform.right;
// direction object's local forward should face
Vector3 forwardDir = Vector3.Cross(downDir, rightDir);
transform.rotation = Quaternion.LookRotation(forwardDir, -downDir);
lastPosition = transform.position;
}