A Free Simple Smooth Mouselook + FreeLook View

Hi, i’ve found this script on the forum:

A Free Simple Smooth Mouselook

And i would to implement a “FreeLook View” that allow me to rotate camera to left and right without changing player direction when i press a button. When i release the button rotation of camera should return to match the player rotation.

How i can do that?

Anyone please?

Edit: i’ve create this for my own and i’m at good point the script works very well but i need a way to add smothing along X Axes

using UnityEngine;

public class SmoothCameraMovements : MonoBehaviour
{
	
	//Variables

	[Range(0.1f, 10f)]
	public float sensitivity = 5f;

	[Range(0.01f, 1f)]
	public float smoothing = 0.05f;

	[Range(0.01f, 1f)]
	public float ReturnSmoothing = 0.05f;

	public Vector2 clamp = new Vector2(70, 70);

	public bool inFreeLookMode = false;
	public bool invertY = false;

	public KeyCode FreeLookKey = KeyCode.F;
	
	public Vector2 RotationVelocity;

	private Vector2 _mouse;	
	private Vector2 _smooth;	

	// Use this for initialization
	void Start ()
	{
	}
	
	
	// Update is called once per frame
	void Update ()
	{
		if ( Input.GetMouseButtonUp( 0 ) )
			Screen.lockCursor = true;

		if( Input.GetKeyDown( KeyCode.Escape ) )
			Screen.lockCursor = false;

		// Getting Mouse axis
		_mouse.x += Input.GetAxis("Mouse X") * sensitivity;
		_mouse.y += (Input.GetAxis("Mouse Y") * sensitivity) * (invertY ? 1 : -1);

		// Clamp along Y axis
		_mouse.y = Mathf.Clamp (_mouse.y, -clamp.y, clamp.y);

		if (Input.GetKeyDown(FreeLookKey))
		{
			if (rigidbody)
				rigidbody.freezeRotation = inFreeLookMode = true;

			// Here we don't increment targetRotation.x because we want to reset rotation when we aren't in Free Look Mode
			_mouse.x = Input.GetAxis ("Mouse X") * sensitivity;

			// Smoothing
			_smooth.x = Mathf.SmoothDamp( _smooth.x, _mouse.x, ref RotationVelocity.x, smoothing );
			_smooth.y = Mathf.SmoothDamp( _smooth.y, _mouse.y, ref RotationVelocity.y, smoothing );
		}
		else if (Input.GetKey(FreeLookKey))
		{
			// Clamp along X axis because we are in Free Look Mode
			_mouse.x = Mathf.Clamp( _mouse.x, -clamp.x, clamp.x );

			// Smoothing
			_smooth.x = Mathf.SmoothDamp(_smooth.x, _mouse.x, ref RotationVelocity.x, smoothing );
			_smooth.y = Mathf.SmoothDamp(_smooth.y, _mouse.y, ref RotationVelocity.y, smoothing );

			// Here we are in Free Look Mode so we can rotate camere around both axis
			transform.localRotation = Quaternion.Euler ( _smooth.y, _smooth.x, 0 );

			// But we must lock player rotation
			transform.parent.transform.Rotate(0, 0, 0);
		} 
		else if (Input.GetKeyUp (FreeLookKey))
		{
			if (rigidbody)
				rigidbody.freezeRotation = inFreeLookMode = false;

			// Reset target to zero to match player view
			_mouse = new Vector2(0f,0f);
		}
		else
		{

			// Smoothing
			_smooth.x = Mathf.SmoothDamp (_smooth.x, 0, ref RotationVelocity.x, smoothing);
			_smooth.y = Mathf.SmoothDamp (_smooth.y, _mouse.y, ref RotationVelocity.y, smoothing);
			Debug.Log(Mathf.Approximately(_smooth.x, 0));
			// Rotate the camera from old angles to the current player direction for X Axis and to 0 degrees along Y Axis relative to parent
			transform.localRotation = Quaternion.Euler (_smooth.y, _smooth.x, 0);


			// Here i need to add smoothing
			transform.parent.transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivity , 0);
		}
	}
}

Here i need to add smoothing and the problem is that i can’t use _smooth.x anymore because i must find a method to wait smoothdamp for completing

transform.parent.transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivity , 0);

Bump?

So, I am not quite getting what you want.

I went in and tested it, and I think you are missing some logic. You need to rotate the parent only when you are not in free look mode. As well, none of this code needs to run if the cursor is locked. (furthermore, if the cursor is not locked, it also needs to be visible. and vice versa)

I rewrote it to get rid of alot of the conditional junk and re-posting of code. I kept the smoothing and attached new smoothing for the parent. And stopped it’s damping and smoothing effects when you go into cursor mode or free look mode. You could cancel the free look mode when you go into cursor mode. That would be the only change I would make.

using UnityEngine;

public class SmoothCameraMovements : MonoBehaviour{
	//Variables
	
	[Range(0.1f, 10f)]
	public float sensitivity = 5f;
	
	[Range(0.01f, 1f)]
	public float smoothing = 0.05f;

	public Vector2 xclamp = new Vector2(-180, 180);
	public Vector2 yclamp = new Vector2(-70, 70);
	
	public bool inFreeLookMode = false;
	public bool invertY = false;
	
	public KeyCode FreeLookKey = KeyCode.F;
	
	public Vector2 RotationVelocity;
	
	private Vector2 _mouse;
	private Vector2 _smooth;

	private float _pSmooth;
	private float pVelocity;
	
	// Use this for initialization we should be looking to start?
	void Start (){
		Screen.lockCursor = true;
		Screen.showCursor = false;
	}

	void Update(){
		// if we are not holding the right mouse button down, unlock the cursor
		if( Input.GetKeyDown( KeyCode.Escape ) ){
			Screen.lockCursor = false;
			Screen.showCursor = true;

			pVelocity = 0;
			_pSmooth = 0;
		}
		if ( Input.GetMouseButtonUp( 0 ) ){
			Screen.lockCursor = true;
			Screen.showCursor = false;
		}
		if ( !Screen.lockCursor) return;


		if(Input.GetKeyDown (FreeLookKey)) inFreeLookMode = !inFreeLookMode;

		// Getting Mouse axis
		_mouse.y += (Input.GetAxis("Mouse Y") * sensitivity) * (invertY ? 1 : -1);

		if (inFreeLookMode) {
			// free look means we don't rotate the parent, just the camera
			_mouse.x += Input.GetAxis("Mouse X") * sensitivity;
			pVelocity = 0;
			_pSmooth = 0;
		} else {
			// no free look, dont use mouse.x;
			_mouse.x = 0;
			_pSmooth = Mathf.SmoothDamp( _pSmooth, Input.GetAxis("Mouse X") * sensitivity, ref pVelocity, smoothing);
			transform.parent.transform.Rotate(0, _pSmooth , 0);
		}
		
		// Clamp axis
		_mouse.x = Mathf.Clamp (_mouse.x, xclamp.x, xclamp.y);
		_mouse.y = Mathf.Clamp (_mouse.y, yclamp.x, yclamp.y);

		// smooth the camera
		_smooth.x = Mathf.SmoothDamp( _smooth.x, _mouse.x, ref RotationVelocity.x, smoothing );
		_smooth.y = Mathf.SmoothDamp( _smooth.y, _mouse.y, ref RotationVelocity.y, smoothing );

		// as much as I love eulers... I hate them.
		transform.localRotation = Quaternion.identity;
		transform.Rotate(_smooth.y, _smooth.x, 0);
	}
}

Nice work, works well. But this is in a “Toggle” way. This can be a drastic solution. If you see my script in game, u could see that when you release the button the free look view return to match the “Player view”

Edit: Sorry, your code do the same :slight_smile:

i’ve just changed this line

if(Input.GetKeyDown (FreeLookKey)) inFreeLookMode = !inFreeLookMode;

with:

if(Input.GetKey(FreeLookKey))
	inFreeLookMode = true;

if(Input.GetKeyUp(FreeLookKey))
	inFreeLookMode = false;

and now works as well. Thank you, i’m reading your code and i attempt to understand what i have missed