- Home /
Quaternion.LookRotation upwards direction of objects is flipping
Hi everyone,
I currently have a problems with the upwards direction of GameObjects when using Quaternion.LookRotation.
In my game (a 2D game using 3D meshes), I have space ships orbiting a planet. In order to calculate the movement while orbiting, I'm using the Transform.RotateAround() method. When the space ships enter the orbit, I need to align their rotation so that they look into the rigt direction (the one they are flying in during orbit).
When entering orbit, I set the rotation using this code:
var dummyObj = new GameObject();
dummyObj.transform.position = unit.transform.position;
dummyObj.transform.RotateAround(planet.transform.position, Vector3.back, 0.1f);
var rotation = Quaternion.LookRotation(dummyObj.transform.position - unit.transform.position, Vector3.up);
unit.transform.rotation = rotation;
Destroy(dummyObj);
I'm creating a dummy object with the same position my ship has. Then I rotate it a tiny bit around the planet. After that, I let the ship look at the dummy object and delete it afterwards.
In about 50% of the cases this works just fine but in the other cases, the botton of the ship gets turned towards the camera instead of the top. I read loads of other posts about the LookRotation method, but couldn't find an answer which solved my problem...
Thanks in advance for your replies!
Since you use Vector3.back in your rotate around function, I would think you would want to do the same thing for your LookRotation. The second parameter of LookRotation defines, in this case, the direction that the top of your ship should point. If you want the top of the ship to face out from the screen (z-axis), then use Vector3.back (like you do in your RotateAround). If you want the ship's top to face "away" from the planet, you could use something like ship.position - planet.position.
I see your point, but finding the right parameters for Quaternion.LookRotation is not the problem. Vector.up seems to do the trick most of the time (if this is unusual maybe my meshes have a weird orientation).
The problem is that around half of the ships are rotated as I want them to be (with their top pointing to the camera) but the other half is somehow rotated just the other way around (with their bottom pointing towards the camera).
I have the same problem when using LookRotation for traveling of ships between planets where ALL traveling ships flip their orientation (from bottom to top pointing towards camera or the other way around) at a certain coordinate (i call LookOrientation every render cycle there). So i got the suspicion there might be a problem with the first input parameter...?
the first parameter looks ok to me.. it is defining the direction and magnitude of the distance you are about to travel, right? (velocity)
Since you didn't mention it I assume it is else-where in your code and working: but I don't see where you set the new position of the ship.. is it something like:
unit.transform.position = dummyObj.transform.position
Edit, just to clarify, on which two axis, do the ships move around?
Your answer
Follow this Question
Related Questions
Bullet rifling simulation 1 Answer
LookRotation is Returning Weird Values 0 Answers
Rotating On Two Axes Independantly 0 Answers
Rotating about and only one axis 1 Answer
Need help with sprite rotation 0 Answers