如何将一个物体的局部轴旋转到另一个物体?

How to rotate a local axis of an object to another object?

使用下面的代码我可以将本地 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); 
}

Update 方法的结果如下:

现在我需要使用另一个轴面对对象,而不是对象的 Z 轴;例如,我会使用正 X 轴来获得此结果:

如何修改我的脚本? 不幸的是,我不知道四元数和向量的数学,我有点困惑。

您可以再追加一次旋转:

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