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". enter image description here
But then, it rotates the axis z for -90 and i dont know why. 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);
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. 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 .
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. 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:
Vector3 dir = target.position - transform.position;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(dir.x, 0, dir.z));
partToRotate.rotation = lookRotation;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.