Unity Cant获取对象以查看其他对象?总是面对左?

时间:2018-10-26 18:51:52

标签: c# unity3d animation quaternions

我不知道这里发生了什么,但需要一个解决方案。这是一个非常简单的任务-我有一个空的容器,其中包含我使用ITween随处移动的电枢/身体网格,并且我需要使身体旋转以面对要移动的对象。

我的问题是我的容器对象内的骨架的z轴朝上,无论出于何种原因,无论使用子骨架还是较大容器的lookAt旋转都偏离了90度,因此对象的左侧面向目标。

enter image description here enter image description here

我尝试过:

"zero"

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

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

当我在目标位置替换为LookRotation时,iTween要么不起作用,要么面朝左。

我如何在上轴上使LookRotation偏移90度?怎么了?

1 个答案:

答案 0 :(得分:1)

您可以将LookRotation的结果乘以修改后的Quaternion以抵消所需的旋转。并且要忍受,可以使用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); 

为避免向上/向下倾斜,您可以将方向清零,然后再将其送入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