How to restrict up and down of a player movement in Unity
Jul 19, 2019
Hello Coder who love animation
Today, I going to share to you how to restrict -+Y axis of movement . Actually this content is pretty easy but sometimes we feel like drank a lot of alcohol and don’t understand anything so lets jump below :)
Only, you have to know Mathf.Clamp(value,min,max). all of other is completely same.
void Update () {
float rotY = Input.GetAxis("Mouse Y");Quaternion camera_rotation = camera.GetComponent<Transform>().localRotation;camera_rotation *= Quaternion.Euler(new Vector3(-rotY, 0, 0));
camera_rotation.x = Mathf.Clamp(camera_rotation.x, -0.3f, 0.3f);
camera.GetComponent<Transform>().localRotation = camera_rotation;}
What do we do?
we add variable from main camera to called camera_rotation. we restrict y axis and finally we override to camera
thats it.