简体   繁体   中英

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.

/// <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:

结果

Now I need to face to the object using another axis, not the Z axis of my object; for example I would use the positive X axis for obtain this result:

在此处输入图片说明

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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