- Home /
Need help in setting a game-object's rotation to euler-degrees.
Hi Guys,
I'm (trying) to make something like a radarsimulator. One radar scans and when it found a target, it hands the targets' vector to the an other script. That other script is supposed to convert the vector to angles and then rotate a game object to face the target. Calculating the angels works out just fine, however, rotating the object to the target doesn't work :-(. Can you guys give me a hand?
Snippet of the code that transforms the vector to degrees and rotates the object:
Vector3 origin;
Vector3 target = new Vector3(0f ,0f ,0f);
float angleX, angleY, angleZ;
public void SetToTargetDirection(Vector3 targetFromMultibeam){
float angleH = 0, angleV = 0;
origin = transform.position;
target = -origin + targetFromMultibeam;
angleH = Mathf.Atan2(target.z, target.x) * Mathf.Rad2Deg;
angleV = Mathf.Atan2(target.y, Mathf.Sqrt((target.x * target.x) + (target.z * target.z))) * Mathf.Rad2Deg;
deltaH = angleH - transform.rotation.eulerAngles.x;
deltaV = angleV - transform.rotation.eulerAngles.y;
transform.Rotate (deltaV, 0, deltaH);
}
I'm away from my desk, so I cannot verify any suggestion I make. As a start try assigning to transform.eulerAngles:
transform.eulerAngles = new Vectore3(angleV, 0, angleH);
Is there a particular reason you're converting to polar and setting angles rather than using Transform.LookAt()?
@ mostlytigerproof I'm using polar because I am simulating the radar by generating game-objects (pulses) which start at the transform.position and move in a certain direction. When they collide with a target, their speed is inversed and they return to the transform.position. The tracking radar then has to send a pulse in the direction of the target (old pulse's direction and distance, recalculated to the trackingradar's position).
I know its a bit jibebrisch, but it's hard to discribe :-).
@ robertbu I'll have a go with that co$$anonymous$$g weekend :-)
@ all, thx for all the feedback!
If I were in your shoes I'd still use direction vectors ins$$anonymous$$d of polar. i.e.
Quaternion rotation = Quaternion.LookRotation(target - origin);
transform.rotation = rotation;
If you need more control, you can blend quaternions using slerp or use ToAngleAxis to get back to polar.
@ mostlytigerproof
I think when using Quaternion.LookRotation, you have to look at a game object (i could very well be wrong though). That is not what I'd like to do. I like to rotate the gameobject in such a way it is facing the same direction as the Vector that is calculated. That's why I'm converting to degrees. However, rotating over these degree values isn't working.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143026im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mimic rotation from a joystick 1 Answer
Is there a way to convert a euler vector from 0 to 360 --> -90 to 90? 0 Answers
Rotation on Android vs. PC 0 Answers
Camera Script Rolling on the Z 3 Answers
Help with gun accuracy in degrees. 3 Answers