- Home /
Always rotate the camera to be stood close to upright.
Hi, I have a faux gravity system set up and the player can control a character collider around a sphere. The camera is attached to this and looks down onto the surface of the sphere (textured as the earth). The problem is that while the player can shift the position of the character controller up and down the surface as well as left and right (strafing), they can eventually lose the correct orientation.
For example, the camera could be looking at Italy on the sphere surface texture just fine but once the player has moved them around the planet, the boot-shaped Italy can become rotated. A better example is Australia, after moving up past the north pole, the sphere has effectively started to flip upside down and upon reaching Australia, the view would show it flipped around.
I need to be able to keep the camera or character controller in the right orientation. Obviously, the character's rotation and position are constantly changing when moving around the sphere, but I am wondering if there is a way to get the character to rotate to have the right 'map' orientation, i.e as with a compass needle always facing the north pole.
Doesn't LookAt (worldPosition : Vector3, worldUp : Vector3 = Vector3.up)
work properly? It's supposed to keep the up orientation of the looking object along the specified up vector.
Another option could possibly be rotating the sphere as opposed to moving the player, but that could cause no less trouble so I'm not sure if it's worth trying. Just tossing the idea there.
Thankyou, I shall have a look at LookAt ;)
Also, would you care to submit your comment as an answer so that if it works I can give you the relevant praise and put this question to bed?
hmmm it did not seem to have the desired effect. I added it to the faux gravity script on the character controller. It does seem to make the character controller look at an object I have placed above the northern pole but as the camera is attached, the player's view is pretty bad.
Answer by hathol · May 15, 2012 at 11:11 PM
You could check the dot product of the camera.transform.up vector and Vector3.up. If the result is negative (= the angle in between the vectors is > 90) then flip the camera (rotate 180 degrees around transform.forward) so that it's up vector is actually pointing upwards again.
I have taken your advice and tried to incorporate a check against the dot product. I have the following in the FixedUpdate of my player's fauxgravity script.
Vector3 toPole = northPole.position - transform.position; Vector3 forward = transform.TransformDirection(Vector3.forward);
if(Vector3.Dot(forward, toPole) < 0)
{
this.transform.RotateAround(transform.forward, 180);
}
where northPole is the transform of a cube positioned at the top of the sphere.
Problem is, past a certain point the camera will constantly rotate in a spasm.
Probably because your forward vector never changes, so it just keeps rotating. Did you try
if(Vector3.Dot(transform.up, Vector3.up) < 0)
{
this.transform.RotateAround(transform.forward, 180);
}
?
just tried this and it still repeats the process over and over, is it because i have it in FixedUpdate?
Hm. Is that script attached to your controller or the camera itself? If it's on the controller, attach it to the camera ins$$anonymous$$d. Whether you execute it in Update or FixedUpdate shouldn't make much of a difference then...
If the script needs to stay on the controller for some reason, get a reference to the camera and use camera.transform.up and camera.transform.forward
Hmmm, this seems to have some effect but the problem is the character controller still remains in the same orientation so the controls become inverted from the player's viewpoint.
The rotation when switching is also instant. I need the camera to always stay at a certain orientation so as to always point upwards to the pole.