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);