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.

Thanks This helped me lot

I managed to get the project back to a working state. I've set up a git repo and moved the project assets into it. If you'd like to take a look at the source code to fix the issue, I would really appreciate it. Since It's a private BitBucket project, I would need to give your account access, instead of just sending you the link

I'm really sorry but I don't really have time for that. I'm happy to help on here and try to give you ideas for things to look for in this, but what you're suggesting there is a bit too involved and time-consuming. I only meant that you should use some kind of version control for your own sake.

2 Answers

2

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

How can i Convert this to JavaScript

I tried doing this and combining it with my movement script but it wont work. The camera always reverts back to 0 after Update()

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