首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >团结能让对象去看其他对象吗?总是朝左看?

团结能让对象去看其他对象吗?总是朝左看?
EN

Stack Overflow用户
提问于 2018-10-26 18:51:51
回答 1查看 1.7K关注 0票数 1

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

我的问题是,我的容器对象内部的电枢有z轴,无论出于什么原因,无论是子电枢还是更大的容器,lookAt旋转都是90度,因此对象的左侧是面向目标的。

我试过:

代码语言:javascript
复制
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是电枢:

代码语言:javascript
复制
 iTween.RotateTo(mainModel, iTween.Hash(
              "z", this.gameObject.transform.eulerAngles.x + 90,
              "time", 1f,
              "easetype", "easeInOutSine",
              "looptype", "pingpong"
            ));

当我在目标处用LookRotation替换时,LookRotation要么不能工作,要么面向左。

如何使LookRotation在向上轴上偏移90度?这里怎么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-26 19:18:35

您可以将LookRotation的结果与修改的Quaternion相乘,以抵消所需的旋转。为了获得更高的转速,您可以使用RotateTowards来设置适当的最大转速。

代码语言:javascript
复制
// 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之前将其归零。

代码语言:javascript
复制
// 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53014742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档