简体   繁体   English

如何将对象的局部轴旋转到另一个对象?

[英]How to rotate a local axis of an object to another object?

Using the following code I can rotate the local Z axis to another GameObject.使用以下代码,我可以将本地 Z 轴旋转到另一个游戏对象。

/// <summary>
/// Faces local Z axis to another target object.
/// </summary>
/// <param name="target">Target.</param>
private void FaceTo(GameObject target){
    float damping = 0.03f;
    var lookPos = target.transform.position - transform.position;

    var rotation = Quaternion.LookRotation(lookPos);

    transform.rotation = 
         Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); 
}

the result during the Update method is the following: Update 方法期间的结果如下:

结果

Now I need to face to the object using another axis, not the Z axis of my object;现在我需要使用另一个轴来面对对象,而不是我对象的 Z 轴; for example I would use the positive X axis for obtain this result:例如我会使用正 X 轴来获得这个结果:

在此处输入图片说明

How can I modify my script?如何修改我的脚本? Unfortunately I do not know the math of Quaternions and Vectors and I'm a bit confused.不幸的是我不知道四元数和向量的数学,我有点困惑。

您可以附加另一个旋转:

var rotation = Quaternion.LookRotation(lookPos) * Quaternion.AngleAxis(-90, Vector3.up);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM