Mouselook 90° Z-angle

I have to rotate my camera 90 degrees and be able to look with mouse. I got it working with 180° angle, but not 90°.

http://dl.dropbox.com/u/70186988/Problem.html ← Unity webplayer

You can probably see what is problem if you try that demo.

Code:

RotationZ is 90° on those scripts. (Applied on another script)

Script1 (This is attached to camera):

function Update(){
Limiter(); //This is just for limiting angle, don't mind about this
rotationX = script2.rotationX;
rotationY += Input.GetAxis("Mouse Y") * sensitivity;
rotAngle = Quaternion.Euler(Vector3(rotationX,rotationY,rotationZ));
transform.rotation = rotAngle;
}

Script2 (This is attached to player model):

function Update(){
Limiter(); //Still just limiting.
rotationX += Input.GetAxis("Mouse X") * sensitivity; 
rotAngle = Quaternion.Euler(Vector3(rotationX,90,rotationZ));
transform.rotation = rotAngle;

}

I can give you more information if u need. I have been thinking this problem for a while and can’t understand what is wrong.


EDIT:

Click attachments to see bigger picture. Playerrota = script2, you don’t probably need moving script information but it came with screenshots. Mouselook = script1.

A Vector3 goes “X,Y,Z”. You’re applying a 90 degree rotation to the Y axis, not the Z axis.

You need to rotate your camera pivot around its local X axis to your target heading on the YZ plane, and then rotate it around its default up axis (probably ‘character’s head up’).

What you’re doing now is rotating around local X and then local Y, after local Y is already pointing not-up. This makes it spin around a weird axis and gets you that odd look.

Generally you want to just parent your camera directly to your character, allow your character to rotate around its Y axis and only allow the camera to rotate on its own local X axis.

In an object hierarchy, rotations depend and influence each other. Also, there are certain limitations when using Euler angles.

For example, if you fly to the North pole, your latitude approaches 90°. But once you are past the North pole, it doesn’t go over 90°. Instead, it decreases again, but your orientation flips around, and your longitude flips by 180°. For a similar reason, you cannot set the X rotation in Unity larger than 90, or smaller than -90(=270). If you try it nonetheless, you will get strange behaviour.

I’m not sure what made you choose mouseX for your player, as this would tilt your whole player forward/backwards, while the Camera looks around left/right.

Instead, the usual approach is to have the Player turn left/right, and then use the Camera to loop up/down, without modifying the player. Combine this with local rotations instead of global ones, and you get this:

  • Player: keep X rotation at 0 and Z rotation at 90. Use MouseY as Y rotation.
  • Camera: keep both X and Z local rotation at 0! Use MouseX as Y(!) rotation.

So in your Camera script, use this:

rotationX += Input.GetAxis("Mouse X") * sensitivity;
transform.localEulerAngles=Vector3(0,rotationX,0); // a) local, b) eulerAngles, c) X as Y angle

And Player would be:

rotationY += Input.GetAxis("Mouse Y") * sensitivity;
transform.localEulerAngles=Vector3(0,rotationY,90);

Not sure what Limiter() does, but chances are it messes with this setup, so doublecheck it.

Note the Camera script is for rotationZ==90, it won’t work like this for other Z orientations. If you want to modify the Z angle in your game, you first have to consider what should be affected - should the player reorient itself, or just the camera? Are you maybe trying to “walk on walls” etc.?