x


Free Look Camera

I'm attempting to make a free look camera (like you might find in Counter Strike after dieing), but I'm having a problem of when I look around while moving, my camera will sometimes just flip over. How can I make it so the camera stays relatively flat while looking around?

Here is what I'm using now:

var turnSpeed = 10.0;
var moveSpeed = 10.0;
var mouseTurnMultiplier = 1;

private var x : float;
private var z : float;
function Update () 
{
    // x is used for the x axis.  set it to zero so it doesn't automatically rotate
    x = 0;

    // check to see if the W or S key is being pressed.  
    z = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed * 10;

    // Move the character forwards or backwards
    transform.Translate(0, 0, z);

    zx = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed * 10;

    // Move the character forwards or backwards
    transform.Translate(zx, 0, 0);

    // Get the difference in horizontal mouse movement
    x = Input.GetAxis("Mouse X") * turnSpeed * mouseTurnMultiplier/10;
    y = Input.GetAxis("Mouse Y") * turnSpeed * mouseTurnMultiplier/10;

    // rotate the character based on the x value
    transform.Rotate(-y, x, 0); 
}
more ▼

asked Dec 02 '10 at 06:49 PM

WasD gravatar image

WasD
4 4 4 7

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Simplify your script just to manage movement, then add the MouseLook script from the standard assets library and voila!

more ▼

answered Dec 02 '10 at 07:20 PM

Rennat gravatar image

Rennat
664 5 8 18

I'll give that a try, thanks.

Dec 02 '10 at 07:36 PM WasD

I'm still getting some tilt, but it's not as bad. I'll just live with it for now.

Dec 02 '10 at 07:45 PM WasD
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3001
x2087
x84
x65

asked: Dec 02 '10 at 06:49 PM

Seen: 2371 times

Last Updated: Dec 02 '10 at 06:49 PM