Unity 拿不到 object 看看其他的 object?总是脸朝左?

Unity Cant get object to look at other object? always faces to left?

我不知道这里发生了什么,但需要一个解决方案。这是一个非常简单的任务——我有一个空的,其中包含一个 armature/body 网格,我使用 ITween 移动它,我需要 body 旋转以面对它正在移动的 object。

我的问题是我容器内的骨架 object 的 z 轴向上,并且无论出于何种原因,对 child 骨架或更大的容器使用任何 lookAt 旋转都会偏离 90 度,因此object 的左侧正对着目标。

我试过:

Vector3 relativePos = test.transform.position - transform.position;
        Quaternion rotation = Quaternion.LookRotation(relativePos);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 1f);

我一直在尝试同时使用 LookAt 和 iTween,none 工作。我知道 iTween 正在旋转,因为这会使 object 在 z 轴上旋转 90 度(我只需要 object 面对 Vector3.up 轴上的目标 - 其中 mainModel是电枢:

 iTween.RotateTo(mainModel, iTween.Hash(
              "z", this.gameObject.transform.eulerAngles.x + 90,
              "time", 1f,
              "easetype", "easeInOutSine",
              "looptype", "pingpong"
            ));

当我在目标处用 LookRotation 替换时,iTween 不起作用,或者面向左侧。

如何让 LookRotation 在上轴上偏移 90 度?这里有什么问题?

您可以将 LookRotation 的结果乘以修改 Quaternion 来抵消您需要的旋转。要获得 lerping,您可以使用 RotateTowards 设置适当的最大旋转速度。

// Find out where we want to look    
Quaternion targetRotation = Quaternion.LookRotation(test.transform.position 
                                                    - transform.position);
targetRotation *= Quaternion.Euler(0f, 90f, 0f); // might need to be -90f


// Find out how far to rotate
float maxDegreesPerSecond = 90f; // This is your max speed for rotation in degrees/sec
float maxDegreesDelta = maxDegreesPerSecond * Time.deltaTime; 
transform.rotation = Quaternion.RotateTowards(transform.rotation,
                                                        targetRotation,
                                                        maxDegreesDelta); 

为避免倾斜 up/down,您可以在将方向输入 LookRotation 之前将方向归零:

// Find out where we want to look 
Vector3 targetDirection = test.transform.position - transform.position;
targetDirection = new Vector3(targetDirection.x, 0f, targetDirection.z);

Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
targetRotation *= Quaternion.Euler(0f, 90f, 0f); // might need to be -90f