Hello everyone, as a part of a game i would like to include a turret that should aim to a vector 3 point, I don´t use the “Look At” script with unity because I would like to add some other parameters but most importantly for learning purposes. Anyway the script i wrote seems to not be working properly, i´ve given it several diferent positions and in most i get two errors saying the following: “!CompareApproximately (SqrMagnitude (q), 1.0F)
UnityEngine.Transform:set_eulerAngles(Vector3)” and “transform.rotation assign attempt for ‘Turret_81’ is not valid. Input rotation is { NaN, NaN, NaN, NaN }.” I dont realy understand why, Here is the code im using
float relativeDistX = point.x - transform.position.x;
float relativeDistY = point.y - transform.position.y;
float relativeDistZ = point.z - transform.position.z;
Vector3 eulerRot = Vector3.zero;
eulerRot.x = Mathf.Atan(Mathf.Sqrt(Mathf.Pow(relativeDistX, 2) + Mathf.Pow(relativeDistZ, 2)) / relativeDistY);
eulerRot.y = Mathf.Atan(relativeDistZ / relativeDistX);
print ("rot" + eulerRot);
clone.transform.eulerAngles = eulerRot;
this is just the way i could think of doing it but if you have a better option please tell me. Thank you all.
p.s the reason the error says “Turret_81” its because it creates the turrets and names in runtime them but that is also weird since there is only one turret.