- Home /
Restrict the mouse posisition
Hey guys,
I was wondering how I would restrict how far the mouse can go on the screen... I was thinking about using mathf.clamp, but I don't really know how... Anyway, if you guys can help, that would be great!
//Written By/For Gibson Bethke var crosshairTexture : Texture2D;
function Update () { Screen.showCursor = false; }
function OnGUI () { var mousePos = Event.current.mousePosition; GUI.DrawTexture( Rect( mousePos.x - (crosshairTexture.width/2), mousePos.y - (crosshairTexture.height/2), crosshairTexture.width, crosshairTexture.height), crosshairTexture); }
Just incase I wasn't clear, I want the mouse to stop before it hits the sides of the screen, much like this (but with the mouse):
//Written By/For Gibson Bethke var normalMoveSpeed : float = 3.125; var topMoveSpeed : float = 6.25; var currentSpeed : float = 3.125;
function Update () { var down = Vector3(0, -1, 0);
if(Input.GetButton("Jump")) {
currentSpeed = topMoveSpeed;
}else{
currentSpeed = normalMoveSpeed;
}
if(Input.GetButton("W")) {
transform.Translate(Vector3.up * currentSpeed);
}
if(Input.GetButton("A")) {
transform.Translate(Vector3.left * currentSpeed);
}
if(Input.GetButton("S")) {
transform.Translate(Vector3.down * currentSpeed);
}
if(Input.GetButton("D")) {
transform.Translate(Vector3.right * currentSpeed);
}
var xMove : float = Input.GetAxis("Horizontal") * Time.deltaTime * 20;
transform.Translate(Vector3(xMove,0,0));
transform.position.x = Mathf.Clamp(transform.position.x, 300, 1700);
var zMove : float = Input.GetAxis("Vertical") * Time.deltaTime * 20;
transform.Translate(Vector3(zMove,0,0));
transform.position.z = Mathf.Clamp(transform.position.z, 300, 1700);
}
-Thanks so much for your help!
P.s. Both the scripts work, the top one is what I have so far
Answer by Statement · Mar 10, 2011 at 06:04 PM
Set minX, maxX, minY, maxY to the values you want it to clamp between.
mousePos.x = Mathf.Clamp(mousePos.x, minX, maxX);
mousePos.y = Mathf.Clamp(mousePos.y, minY, maxY);
function OnGUI () { var mousePos = Event.current.mousePosition;
mousePos.x = Mathf.Clamp(mousePos.x, minX, maxX);
mousePos.y = Mathf.Clamp(mousePos.y, minY, maxY);
GUI.DrawTexture( Rect( mousePos.x - (crosshairTexture.width/2),
mousePos.y - (crosshairTexture.height/2),
crosshairTexture.width,
crosshairTexture.height), crosshairTexture);
}
Well, you need to have a mousePos. Just add it where you have one.
Do you think you could answer one more question? How would I restrict the mouse's movement speed?
I guess you could sample the position and store it to the next frame. Then use Vector3.Clamp$$anonymous$$agnitude on the delta. http://unity3d.com/support/documentation/ScriptReference/Vector3.Clamp$$anonymous$$agnitude.html
Answer by Rakshaan · May 24, 2021 at 09:57 AM
easy way I use to restrict cursor to a distance from edges of screen: use the newMousePos as your cursor value.
public float mousePosX_min;
public float mousePosX_max;
public float mousePosY_min;
public float mousePosY_max;
void Update()
{
float mousePosX = Mathf.Clamp(Input.mousePosition.x,mousePosX_min, mousePosX_max);
float mousePosY = Mathf.Clamp(Input.mousePosition.y,mousePosY_min, mousePosY_max);
newMousePos = new Vector2(mousePosX,mousePosY);
}
float mousePosX = Mathf.Clamp(Input.mousePosition.x, minClamp, maxClamp); float mousePosY = Mathf.Clamp(Input.mousePosition.y, minClamp, maxClamp); Vector2 newMousePos = new Vector2(mousePosX, mousePosY); mousePosition = newMousePos;
nothing happened