简体   繁体   English

Quaternion.Euler 旋转错误的轴

[英]Quaternion.Euler rotates wrong axis

I have this problem where i have this: enter image description here我有这个问题:在此处输入图像描述

And then, i tried to rotate the y axis and that the "PartToRotate" follows the "enemy".然后,我尝试旋转 y 轴,并且“PartToRotate”跟随“敌人”。 enter image description here在此处输入图像描述

But then, it rotates the axis z for -90 and i dont know why.但是随后,它将轴 z 旋转了 -90,我不知道为什么。 enter image description here在此处输入图像描述

Therefore i appreciate if you can help me with this.因此,如果您能帮助我,我将不胜感激。 I leave all the code here:我把所有代码都留在这里:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Turret : MonoBehaviour
{

    private Transform target;
    public float range = 15f;
    public Transform partToRotate;

    public string enemyTag = "Enemy";

    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("UpdateTarget", 0f, 0.5f);
    }

    void UpdateTarget()
    {
        GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
        float shortestDistance = Mathf.Infinity;
        GameObject nearestEnemy = null;

        foreach ( GameObject enemy  in enemies)
        {
            float distanceToEnemy = Vector2.Distance(transform.position, enemy.transform.position);
            if(distanceToEnemy < shortestDistance)
            {
                shortestDistance = distanceToEnemy;
                nearestEnemy = enemy;
            }
        }
        
        if(nearestEnemy != null && shortestDistance <= range)
        {
            target = nearestEnemy.transform;
        }
        else
        {
            target = null;
        }
        
    }

    // Update is called once per frame
    void Update()
    {
        if (target == null)
        {
            return;
        }

        Vector3 dir = target.position - transform.position;
        Quaternion lookRotation = Quaternion.LookRotation(dir);
        Vector3 rotation = lookRotation.eulerAngles;
        partToRotate.rotation = Quaternion.Euler(0f, rotation.y, 0f);

    }

    void OnDrawGizmosSelected ()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, range);
    }

}

Here are the photos.这是照片。 enter image description here .在此处输入图像描述 thats how looks before, and the idea with the new code Vector3 direction = target.position - transform.position; float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; partToRotate.rotation = Quaternion.Euler(0, 0, angle);这就是以前的样子,以及新代码Vector3 direction = target.position - transform.position; float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; partToRotate.rotation = Quaternion.Euler(0, 0, angle); Vector3 direction = target.position - transform.position; float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; partToRotate.rotation = Quaternion.Euler(0, 0, angle); is that the "partToRotate" follows the enemy (target), and he do that, but it still get (plus the z, that is the axis i want) the x axis.是“partToRotate”跟随敌人(目标),他这样做了,但它仍然得到(加上z,这是我想要的轴)x轴。 enter image description here在此处输入图像描述

Without seeing the values for target.position and transform.position can't ensure it but you may be experiencing a gimbal lock .如果没有看到target.positiontransform.position的值,则无法确保它,但您可能遇到了万向节锁定

To avoid gimbal locking you must avoid euler angles and use only quaternions.为避免万向节锁定,您必须避免欧拉角并仅使用四元数。 In your code you create a quaternion, then you get the euler angles and then you create a new quaternion using only the Y axis of those euler angles.在您的代码中,您创建一个四元数,然后获得欧拉角,然后仅使用这些欧拉角的 Y 轴创建一个新的四元数。 I assume that's to restrict the rotation to be on the XZ plane, you can do the same without using euler angles like this:我假设这是为了限制旋转在 XZ 平面上,你可以不使用这样的欧拉角来做同样的事情:

Vector3 dir = target.position - transform.position;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(dir.x, 0, dir.z));
partToRotate.rotation = lookRotation;

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

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