- Home /
make an object "Look at" an other object
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.
Answer by TheShadyColombian · Jan 29, 2015 at 02:38 AM
Use "lookAt" (how conveniently named :D)
oh thank you very much, i had not thought about that, but i do want to add diferent parameters, such as an x and y rotation cap and a speed. So i think ill use the other way but I very much apreciate your answer!
Alright. Hope I helped you at least a little :)
oh certainly, perhaps not on this particular script but certainly learning or remembering stuff always opens a whole variety of options in the project. Cant be more greatful for people like you that put time and effort to apport knowledge to the community.
Your answer

Follow this Question
Related Questions
Sphere rotation based on object on the sphere 0 Answers
How do you calculate the rotation of a hexagon based on the six Vector3 points 0 Answers
How to make 90 degree snapping camera, center on object? 0 Answers
restrict mouselook X movement script does not work 4 Answers
Limiting Rotation x of Main Camera 1 Answer