Mouse Looking

Hey guys! So I’m new to Unity, but I’m not new to game developing. I have experience in scripting too.
Currently I’m reading the documentation and trying to understand how things work in Unity, however I have a question. I’m trying to create a first person camera control script from scratch, just so I can get familiar with JS in Unity.

So after reading the documentation, I made a one-line script:

transform.Rotate(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);

I was surprised to see how easy it is to make a mouse rotation (only one line!), but there’s a problem. The rotation is local, not global, that’s not OK for a camera :smile:

I understand the structure of programming, but all those functions are pretty new to me, besides, I’ve worked very little with OOP languages.

So how can I do the global rotation here ? I only ask for that, I’ll deal with the angle restrictions and the other stuff later.

You could make an empty GameObject called Pivot, for example, and set the camera as a child.

PivotScript :

function Update ()
{
	transform.RotateAround (Vector3.up, Input.GetAxis ("Mouse X"));
}

CameraScript :

function Update () {
	transform.Rotate (Vector3.right, Input.GetAxis ("Mouse Y"));
}

Yeah, that’s actually a good idea. In first person games, the camera rotates the player and does the pitch by itself.

Thanks!

You could have a look at the Island Demo project. Or you could import the standard package (Assets > Import package (Unity main folder / Editor / Standard package / Standard Assets.unitypackage ). There should be a prefab, First Person Controller.

After some reverse engineering, you should be able to understand it :wink:

Yeah, I did already. The problem is, that all the built in things - the components, the classes and everything is new to me and looking an already done script doesn’t really help me understand how it works, because I’m looking at commands, that I don’t actually know how they work :smile:

Check the docs for the Rotate function.

You can specify the relativeTo parameter with Space.World.

So it would be like:

Rotate (Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0), Space.World);

I have a very bad behaviour when using this line, don’t you ? :?

Yep, this way the camera still rotates locally. But I don’t get it…

Is the camera a child of another object?

Nope.

Seeing the Space.World frigtened me, that’s why I didn’t want to use it in first hand ! Space.World, where is the center of the referential ?

The pivot of the game object ?
The center of the game object ?
Or position (0, 0, 0) of the world ?

:?