Rotate the object by the mouse.

Hey everyone!
Listen i have a problem. I’m trying to creat a script that will rotate
me by the mouse. My friend suggested me this script:
the player script:

var speed : float = 10.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
private var controller:CharacterController;
private var myTransform:Transform;

function Awake ()
{
	myTransform = GetComponent(Transform);
	controller = GetComponent(CharacterController);
}

function FixedUpdate() 
{	
	if (controller.isGrounded)
	{
		moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		
		if (Input.GetButton ("Jump"))
		{
			moveDirection.y = jumpSpeed;
		}
	}
	
	//Rotate around the Y Axis
		transform.Rotate(Vector3(0, Input.GetAxis("Mouse X") * 3, 0)); 
	
	// Apply gravity
		moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
		controller.Move(moveDirection * Time.deltaTime);
}

  the camera script:


    var min = -25.0;
var max = 25.0;
private var myTransform:Transform;
private var rotationX:float;

function Awake()
{
	myTransform = GetComponent(Transform);
}

function FixedUpdate()
{
	//Rotate:
		myTransform.Rotate(Vector3(-Input.GetAxis("Mouse Y") * 3, 0, 0));
		
	//Limit rotation:
		rotationX =	transform.localRotation.x;
		print(rotationX);
		
		if(rotationX > 0.3180144)
			transform.localRotation.x = 0.3180144;
		
		else if(rotationX < -0.3180144)
			transform.localRotation.x = -0.3180144;
		
}

However, it does not work so well.
T can’t move and walk around together
I thought about an idea that I will rotate, and then I will just going Straight
(Direction in which I look will be mys Straight)
However, I don’t how to do it.
is that wull work ? and how shuld i so it?

tnx for your help

One of the most common mistakes. Transform.rotation or Transform.localRotation are both Quaternions. The 4 members of a quaternion (x,y,z,w) represents a complex number system. You shouldn't access or change these values unless you know what you're doing.

What you want is the rotation in eulerAngles. The local euler angles are the numbers you can see in the inspector.

Every quaternion has an eulerAngles property to access the angles.

The Transform class however have it's own "shortcuts": Transform.eulerAngles and Transform.localEulerAngles

edit

This should be enough to limit the rotation:

mytransform.localEulerAngles.x = Mathf.Clamp(mytransform.localEulerAngles.x, min, max);

second edit

Well, the range of the rotation is a little problem. That's why you shouldn't do relative rotations. Change the rotationX directly and set an absolute rotation.

Btw. You should use Update() instead of FixedUpdate(). FixedUpdate is only required for physics caluclations when you work with forces. Using mouse-delta-input in FixedUpdate will make it framedependent.

function Update()
{
    rotationX -= Input.GetAxis("Mouse Y") * 3;
    rotationX  = Mathf.Clamp(rotationX, min, max);
    transform.localRotation = Quaternion.Euler(rotationX,0,0);
}