Quaternion.LookAt() without restricting manual rotation
I have to automatically rotate my character every fram according to position he's at using Quaternion.LookAt(). But at the same time I want to be able to rotate him around Z axis, which of course I can't since that value is denied by the LookAt() function. Of course, I can solve that problem using math but maybe there's a simpler way. I tried this
Vector3 direction = planet.transform.position - body.transform.position;
direction = new Vector3(direction.x, direction.y, this.transform.rotation.z);
transform.rotation = Quaternion.LookRotation(-direction);
But it didn't work out.
Answer by Sergio7888 · Sep 24, 2016 at 11:57 AM
rotation.z is Quaternion z value not the z angle to get the z angle use rotation.eulerAngles.z.
rotation is a Quaternion and Quaternion are complex data with value x,y,z,w.
Answer by npoguca · Sep 24, 2016 at 01:46 PM
the answer above made a good point. The solution to the whole problem looks like this
Quaternion actualRotation;
float yRot = 0;
look.LookRotation(out actualRotation);
Vector3 direction = planet.transform.position - body.transform.position;
direction = new Vector3(-direction.x, -direction.y, actualRotation.eulerAngles.z);
transform.rotation = Quaternion.LookRotation(direction); transform.rotation = actualRotation;
This is hardly a solution. All your code is actually doing is this:
Quaternion actualRotation;
look.LookRotation(out actualRotation);
transform.rotation = actualRotation;
Since we don't know what "look" actually is, it's not clear what you actually do. All the lines i omitted have no effect since you overwrite the rotation at the end with "actualRotation".
Apart from that your "direction" makes no sense. It's a direction vector, so using an angle as third component makes no sense.
btw: Quaternion.LookRotation has two parameters. If you omit the second it defaults to Vector3.up which will make the rotations y-axis to point upwards.
If you want the local z rotation to roughly keep it's previous direction you can use
Vector3 direction = planet.transform.position - body.transform.position;
transform.rotation = Quaternion.LookRotation(direction, transform.up);
Your answer
Follow this Question
Related Questions
Using Local Angles to target enemy object 0 Answers
Relative rotation on a custom axis? 0 Answers
How to Rotate Quaternion 180 Relative to GameObject? 0 Answers
3 Axis Camera Rotation 0 Answers