mouse look script

how can i get my camera to follow mouse? can someone please tell me the sript

This is MouseLook.cs:

using UnityEngine;
using System.Collections;

/// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation

/// To make an FPS style character:
/// - Create a capsule.
/// - Add a rigid body to the capsule
/// - Add the MouseLook script to the capsule.
///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSWalker script to the capsule

/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {

    public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityX = 15F;
    public float sensitivityY = 15F;

    public float minimumX = -360F;
    public float maximumX = 360F;

    public float minimumY = -60F;
    public float maximumY = 60F;

    float rotationX = 0F;
    float rotationY = 0F;

    Quaternion originalRotation;

    void Update ()
    {
        if (axes == RotationAxes.MouseXAndY)
        {
            // Read the mouse input axis
            rotationX += Input.GetAxis("Mouse X") * sensitivityX;
            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

            rotationX = ClampAngle (rotationX, minimumX, maximumX);
            rotationY = ClampAngle (rotationY, minimumY, maximumY);

            Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
            Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, -Vector3.right);

            transform.localRotation = originalRotation * xQuaternion * yQuaternion;
        }
        else if (axes == RotationAxes.MouseX)
        {
            rotationX += Input.GetAxis("Mouse X") * sensitivityX;
            rotationX = ClampAngle (rotationX, minimumX, maximumX);

            Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
            transform.localRotation = originalRotation * xQuaternion;
        }
        else
        {
            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
            rotationY = ClampAngle (rotationY, minimumY, maximumY);

            Quaternion yQuaternion = Quaternion.AngleAxis (-rotationY, Vector3.right);
            transform.localRotation = originalRotation * yQuaternion;
        }
    }

    void Start ()
    {
        // Make the rigid body not change rotation
        if (rigidbody)
            rigidbody.freezeRotation = true;
        originalRotation = transform.localRotation;
    }

    public static float ClampAngle (float angle, float min, float max)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp (angle, min, max);
    }
}

I found the script in the accepted answer was giving me some weird rotations so I wrote a pretty minimal script that works for my use case quite well (sitting at a chair and looking around, but you should be able to extend it easily for walking etc.)

using System;
using UnityEngine;

public class MouseLook : MonoBehaviour
{
	public float mouseSensitivity = 100.0f;
	public float clampAngle = 80.0f;

	private float rotY = 0.0f; // rotation around the up/y axis
	private float rotX = 0.0f; // rotation around the right/x axis

	void Start ()
	{
		Vector3 rot = transform.localRotation.eulerAngles;
		rotY = rot.y;
		rotX = rot.x;
	}

	void Update ()
	{
		float mouseX = Input.GetAxis("Mouse X");
		float mouseY = -Input.GetAxis("Mouse Y");

		rotY += mouseX * mouseSensitivity * Time.deltaTime;
		rotX += mouseY * mouseSensitivity * Time.deltaTime;

		rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);

		Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
		transform.rotation = localRotation;
	}
}

I kinda wish Unity had some more minimal components than those in the standard assets. It’s easier to understand and learn from simple code, and then you can extend it yourself, and it’s REALLY nice not to have a bunch of dependencies to pull in :smiley:

MouseLook.cs and other character controllers are available from Unity if you select from the menu bar “Assets” → Import Package → Character Controller

After that, the following will be available from the menu bar:

“Component” → Camera-Control → Mouse Look

“Component” → Character → FPS Input Controller

Allright this one here from AndyP is great.

The way I made it work was:

  1. creating a new empty C# script in my scripts folder.
  2. Renamed the script “MouseLookSimple” (to avoid getting it tangled in with the asset script).
  3. Attached it to my custom player-character (note that I am not using the Assets FPS).
  4. Remember to rename the class in the scrip “MouseLookSimple” in line 4.

Note that the main camera is a child of my Player character.

I’m using AndyP’s script in a third person shooter and when I use it to rotate the character, it doesn’t move in the direction that it is facing, even though I am using transform.localPosition to move it. It could have something to do with the script for the player but I’m only a beginner and wouldn’t really know what’s going wrong anyway.`

    rb = GetComponent<Rigidbody>();

%|1229200423_2|%

// Update is called once per frame

%|1240188770_4|%
%|1793753209_5|%
if (Input.GetKey(“w”))
{
%|-22954854_8|%
%|-1985746889_9|%
transform.Rotate(Vector3.forward * 0);
}
if (Input.GetKey(“s”))
{
;
%|1700589554_15|%
%|1190135946_16|%
}
if (Input.GetKey(“a”))
{
;
transform.localPosition += (Vector3.left * speed);

    }

%|-1911921683_24|%
{
;
transform.localPosition += (Vector3.right * speed);
transform.Rotate(Vector3.back * 0);
%|-185005678_29|%
if (Input.GetKey(“KeyCode.LeftShift”))
{
transform.localPosition += (Vector3.forward * runspeed);
transform.Rotate(Vector3.back * 0);
}

        if (Input.GetKey("KeyCode.Space"))

%|-703131115_36|%
%|1705099323_37|%
%|1692623434_38|%
%|413218732_39|%
}
}

`
The rotation is unnecessary but I might use it in the future so I kept it there.

public float FlyingSpeed = 150;
public float RotationSpeed = 50;
public float LoweringSpeed = 25;

	void Update () {
		if(Input.GetKey(KeyCode.UpArrow))
			transform.Translate (Vector3.back*FlyingSpeed*Time.deltaTime);

		if(Input.GetKey(KeyCode.DownArrow))
			transform.Translate (Vector3.back*-FlyingSpeed*Time.deltaTime);

		if (Input.GetKey (KeyCode.Space))
			transform.Translate (Vector3.down * LoweringSpeed * Time.deltaTime);
				
		if(Input.GetKey(KeyCode.LeftShift))
			transform.Translate (Vector3.down * -LoweringSpeed * Time.deltaTime);
		
		if(Input.GetKey(KeyCode.RightArrow))
			transform.Rotate (Vector3.up*RotationSpeed*Time.deltaTime);

		if(Input.GetKey(KeyCode.LeftArrow))
			transform.Rotate (Vector3.down*RotationSpeed*Time.deltaTime);

	}
}

This script works perfectly for any kind of 3rd person vehicle games… If you want to make it more realistic use physics but even if you are new at this kind of scripts you will be able to understand it!!