简体   繁体   English

使用 transform.LookAt 冻结 Y 旋转?

[英]Use transform.LookAt freezing Y rotation?

[EDIT] Solved with: [编辑] 解决了:

Vector3 relativePos = hit.point - pivot.transform.position; Vector3 rotation = Quaternion.LookRotation(relativePos).eulerAngles; pivot.transform.localEulerAngles = new Vector3(0, 0, rotation.y);

Hello i recently came across a problem.你好我最近遇到一个问题。 I'm trying to make this object to look at my raycast point, without having to rotate it.我试图让这个对象查看我的光线投射点,而不必旋转它。 As yu can see from the picture the script is not working properly.正如您从图片中看到的那样,脚本无法正常工作。

标准的“枪” 改造后的“枪”

The code I'm using:我正在使用的代码:

Ray ray = theCamera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
    RaycastHit hit;
    Physics.Raycast(ray, out hit);
    pivot.transform.LookAt(hit.point);
    pivot.transform.rotation = new Quaternion(0, pivot.transform.rotation.y, 0, 0)

The question: Is possible to make the "blue arrow" to look at the "hit.point" remaining in the same rotation plane?问题:是否可以让“蓝色箭头”查看留在同一旋转平面中的“hit.point”? Sorry for the bad explanation but i'll post aslo an image of the result I'd like to achieve(the red cube is the raycast point).抱歉解释不好,但我会发布我想要实现的结果的图像(红色立方体是光线投射点)。 在此处输入图像描述

I guess you just want to rotate on Y, not X & Z?我猜你只想在 Y 上旋转,而不是 X 和 Z? In any event you should never modify or create a Quaternion out of parts, use the built in methods:在任何情况下,您都不应该修改或创建零件的四元数,请使用内置方法:

Vector3 rotation = Quaternion.LookRotation(relativePos).Euler
public Quaternion rotationAdjusted = Quaternion.Euler(0, rotation.eulerAngles.y, 0);
transform.rotation = rotationAdjusted

See Quternion.Euler and Quaternion.eulerAngles请参见Quternion.EulerQuaternion.eulerAngles

Try something like this:尝试这样的事情:

// Keep the Y component the same as your transform
pivot.transform.LookAt(new Vector3(hit.point.x, this.transform.position.y, hit.point.z));

If what you need is just to rotate the object around the Y axis, in the direction of the hit.point, without affectin X and Z axis...如果您需要的只是围绕 Y 轴旋转对象,在 hit.point 的方向上,而不影响 X 和 Z 轴......

... Then in that case, I believe freezing X and Z rotation will do the trick. ...然后在那种情况下,我相信冻结 X 和 Z 旋转会解决问题。

You can do that in the inspector, or through code:您可以在检查器中执行此操作,也可以通过代码执行此操作:

Rigidbody _rigidbody = GetComponent<Rigidbody>();
_rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ

This is not mistake, as it uses the bitwise OR operator.这没有错,因为它使用了按位或运算符。 Also note that rotation constraints are applied in Local space, not WorldSpace.另请注意,旋转约束应用于本地空间,而不是世界空间。


EDIT: Deleted first unneeded part, as I didn't understand the question from first try :)编辑:删除了第一个不需要的部分,因为我第一次尝试时不理解这个问题:)

In my game the enemy will face towards the player but LookRotation() function also changes the x value which I don't want.在我的游戏中,敌人将面向玩家,但 LookRotation() 函数也会改变我不想要的 x 值。 So I did something like this所以我做了这样的事情

var relativePos = thePlayer.transform.position - transform.position;
var rotation = Quaternion.LookRotation(relativePos).eulerAngles;
transform.rotation = Quaternion.Euler(0, rotation.y, 0);

hope it helps someone.希望它可以帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM