【问题标题】:Unity Cant get object to look at other object? always faces to left?Unity无法让对象查看其他对象?总是面朝左?
【发布时间】:2018-10-26 18:51:51
【问题描述】:

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

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

我试过了:

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

我一直在尝试同时使用 LookAt 和 iTween,但都不行。我知道 iTween 正在旋转,因为这会使对象在 z 轴上旋转 90 度(我只需要对象在 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 度?这里有什么问题?

【问题讨论】:

  • 我知道你想让它成为 lerp/tween,但是当你这样做 transform.LookAt(test.transform.position); 时,唯一的问题是它向左看?
  • 它转了,但是1.用lookAt太突兀但是2.物体的右侧面向目标,而不是前面@Ruzihm

标签: c# unity3d animation quaternions


【解决方案1】:

您可以将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); 

为避免向上/向下倾斜,您可以在将方向输入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

【讨论】:

  • 我怎样才能将它限制在 y 轴(上轴)上?现在它使他稍微向上倾斜
  • @skyguy 我编辑了我的答案,包括如何避免向上/向下倾斜
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-28
  • 2019-04-05
  • 1970-01-01
  • 2012-02-10
相关资源
最近更新 更多