Rotate head with mouse

Hello. I wanna make a mouse look for my character’s head. I tried to use unity’s mouse look for camera. But it make so many problems. How to i can make a mouse look for head?

Watch this video series from quil18creates and you’ll learn how to create an FPS controller from scratch which implements a nice mouse look, very well explained.

But here’s how anyway, create a character controller, remove all the scripts from it, and attach it this:

public class MyMouseLook
{
   public float mouseSensitivity = 5;
   public float maxLookUpDownAngle = 60; // can't look up/down more than 60 dgs
   public Transform cam;

   private Transform _transform;
   private float upDownRotation = 0.0f;
   private float rightLeftRotation = 0.0f;

   void Start()
   {
      _transform = transform; // cache the transform component
      if (cam == null)
      {
   	    cam = Camera.main;
   	    if (cam == null) print("wtf no cam");
      }
   {

   void Update()
   {
	// left/right rotation
	// for that we could rotate the whole character controller left/right
	Vector3 newRotation = new Vector3(0, rightLeftRotation, 0);
	rightLeftRotation += Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
	newRotation = new Vector3(0, rightLeftRotation, 0);
	_transform.localRotation = Quaternion.Euler(newRotation);

	// up/down rotation
	// for looking up/down we can't tilt the character controller so we have to pitch the camera up/down
	// the - is because up is negative angles, down is positive ones
	// and upDownRotation increases/decreases if we move the mouse up/down
	// upDownRotation must be a member field (not local) otherwise it won't work
	upDownRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
	upDownRotation = Mathf.Clamp(upDownRotation, -maxLookUpDownAngle, maxLookUpDownAngle);

	// getting the newRotation vector out of our left/right and up/down mouse values
	// remember we rotate up/down around the x-axis and left/right around the y-axis
	newRotation = new Vector3(upDownRotation, 0, 0);
	cam.localRotation = Quaternion.Euler(newRotation); 
   }
}

Credits goes to quil for this script.

Place this on the camera you are using to follow the head. This is for a camera that is not attached to any other game object…

private const bool X_INVERSE = false;
private const bool Y_INVERSE = false;
private const float SENSITIVITY = 20;

void Start() { }

void Update() {
	PollForAxisAdjustments();
}

private void PollForAxisAdjustments() {
	float xAxisMovement = Input.GetAxis("Mouse X") * SENSITIVITY * Time.deltaTime;
	float yAxisMovement = Input.GetAxis("Mouse Y") * SENSITIVITY * Time.deltaTime;
	
	if (X_INVERSE) {
		xAxisMovement *= -1;
	}
	
	if (Y_INVERSE) {
		yAxisMovement *= -1;
	}
	
	makeXAxisAdjustments(xAxisMovement);
	makeYAxisAdjustments(yAxisMovement);
}

private void makeXAxisAdjustments(float adjustment) {
	if (adjustment == 0) return;
	
	Vector3 currentRotation = this.gameObject.transform.eulerAngles;
	Quaternion newRotation = Quaternion.Euler(currentRotation.x, (currentRotation.y + adjustment), currentRotation.z);
	this.gameObject.transform.rotation = newRotation;
}

private void makeYAxisAdjustments(float adjustment) {
	if (adjustment == 0) return;
	
	Vector3 currentRotation = this.gameObject.transform.eulerAngles;
	Quaternion newRotation = Quaternion.Euler((currentRotation.x - adjustment), currentRotation.y, currentRotation.z);
	this.gameObject.transform.rotation = newRotation;
}