How to get the localEulerAngle Transform.Right that is being set from a world rotation without setting it to a transform.
Basically I'm trying to get the LocalEulerAngle of the base.transform.right position that is being set by the lookAtPos. I want to get this value without the need to set the value to the transform. This way I can do some detection on the LocalEulerAngle before setting it to the transform.
Vector3 lookAtPos = (target.transform.position - base.transform. position).normalized;
base.transform.right = lookAtPos;
Basically I want the LocalEulerAngle of the base.transform.right that is being set by the lookAtPos without having to set the base.transform.right. Preferably using some Vector3 $$anonymous$$ath.
Answer by Glurth · Jan 30, 2018 at 10:20 PM
Not sure exactly what you mean... if you want to make your transform.right face the lookAtPos, you could do something like this:
using UnityEngine;
public class lookatTest : MonoBehaviour {
public Transform lookat;
void Update () {
Quaternion lookAtRot = Quaternion.LookRotation(lookat.position-transform.position);
Quaternion lookRight = Quaternion.LookRotation(-Vector3.right);
Quaternion finalRot = lookAtRot * lookRight;
Debug.Log("finalRotEulers:" + finalRot.eulerAngles);
transform.rotation = finalRot; //sounds like you DONT want to do this, for some reason
}
}
A key in this answer is LookRotation. 'Quaternion.LookRotation(target.pos - transform.pos)` computes the same angle that LookAt(target) would have set. That's actually how LookAt works. You can always use LookRotation to test-compute what LookAt would do.
Hmm.. seems like lookAtRot*Quaternion.Euler(0,90,0) would be simpler to compute the final local-right. Is the original lookAtRot facing backwards on purpose?
Back to the intent, it's safe to set a rotation using LookAt, sample it, then change it back. It's the same as x=3; x=5;. $$anonymous$$aybe it's ugly, but if it makes the game work... .
Good catch, I got both the vectors backwards! Should be lookat.position-transform.position
and -Vector3.right
.. I'll edit the answer.. "Simpler" to specify Euler angles: I like this format because sometimes (not in this case) one might want to use something like this:
q=Quaternion.LookRotation(transform.right);
Your answer
![](https://koobas.hobune.stream/wayback/20220612151622im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotation problem 0 Answers
Euler angles doesn't give correct value. 0 Answers
Problem rotating camera on negative z axis 1 Answer
Limit movement to just forward/backward and left/right 0 Answers