在Unity3D中将对象旋转到另一个 [英] rotate Object towards another in Unity3D

查看:118
本文介绍了在Unity3D中将对象旋转到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在此问题中使用了此功能.我更改了旋转的符号:

I have already this function from this question. I changed the sign of the rotation:

 void rotateBotConnector()
{
    Vector3 diff = (player.transform.position - botConnector.transform.position).normalized;
    float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
    botConnector.transform.localRotation = Quaternion.Euler(0f, 0f, -(rot_z - 90f));
}

但是问题是,现在我的对象在XZ平面上跟随播放器,但是当旋转达到一定程度(左右)时,对象停止向我的播放器旋转.

But the problem is, that now my object follows the player on the XZ plane but when the rotation reaches a certain degree, left or right, the object stops to rotate towards my player.

为了更好地理解: http://imgur.com/vWaqc31

推荐答案

为什么不只使用:

var target : Transform;

transform.LookAt(Vector3(target.transform.position.x, target.transform.position.y, transform.position.z);

这似乎比使用euler容易得多.这样,您可以查看目标的x& y,但变换z.

It seems a lot easier than using euler. This way you look at target's x & y but transform your z.

我也不是欧拉专家,但似乎仅限于90度转弯,我认为这可能是以下原因:

Also I'm no expert with euler but it seems like it is limited to a 90 degree turn and I think this may be the reason why:

Quaternion.Euler(0f, 0f, -(rot_z - 90f));

这篇关于在Unity3D中将对象旋转到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆