- Home /
How can i Limit the Mouse X Axis for Mouse Look
Hello there I need to Limit the Players Rotation on the X Axis. I can Move the mouse and it will rotate the Player but i need to Stop about half way.
Here is what i got so far.
var VerticalSpeed : float = 2.0; var HorizontalSpeed : float = 2.0; var PlayerObject : Transform;
function Update (){ Screen.lockCursor=true; Cursor.visible=true; Screen.lockCursor=false; var h : float = HorizontalSpeed *Input.GetAxis("Mouse X"); var v : float = VerticalSpeed *Input.GetAxis("Mouse Y"); transform.Rotate(-v,0,0); PlayerObject.Rotate(0,h,0); }
First of all, you say Limit the mouse Y axis, then you say limit the rotation on the X axis. Can you be more clear on which axis you need? What do you mean by "half way"? It can mean a lot of things. You want it to only rotate 180 degrees? 180 degrees to the middle of the player?
Be more specific.
Sorry for any Confusion But see i Put y in the Title I meant X not Y my bad. changed it.
I don't want the Player to Be able to look all the Way around just Half way on both sides.
Answer by Soos621 · Jun 01, 2016 at 03:39 PM
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -90F;
public float maximumX = 90F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
float rotationX = 0f;
void Update (){
rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
}
the mathf.clamp will limit the angle of the object to the min or maximum degrees
you will have to convert it manually, i just pulled this from one of my projects but i only code in c# i wouldnt be able to give you an accurate translation, but i know that its not hard, i have had to decipher from js to c#
I tried doing this and combining it with my movement script but it wont work. The camera always reverts back to 0 after Update()
$$anonymous$$ake sure you're setting the currrent rotation in either Start() direct in monobehaviors - not in your movement function probably called every Update().
Answer by Mynameisbec · Jan 26, 2021 at 06:17 AM
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -90F;
public float maximumX = 90F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
float rotationX = 0f;
void Update (){
rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
}