- Home /
Limit Mouse movement around player
I'm working on a 2d top-down shooter, where the player aims using the mouse. I'm trying to limit the mouse movement around the player.
I'm using C# for scripting
EDIT: I forgot to mention that I don't need to use the hardware cursor. Currently in my project I'm hiding the cursor and using a GameObject that follows the mouse input as the crosshair.
First, you can not really control mouse in unity by native, you need to access hardware mouse which can be very tricky for different OS. I can suggest this asset for this task. if you use this asset this would be your sample code :
Vector2 PlayerPosition = Vector2.zero;
float radius = 4f;
Vector2 last$$anonymous$$ousePosition = Vector2.zero;
private void Update() {
float distance = Vector2.Distance(Camera.main.ScreenToWorldPoint(Input.mousePosition),PlayerPosition);
if(distance < radius)
{
last$$anonymous$$ousePosition = Input.mousePosition;
}
else if (distance >= radius)
{
HardwareCursor.SetPosition(last$$anonymous$$ousePosition);
}
}
but as this is not a free asset i will try to give more suggestion to solve this without it.
you can check system.windows.form.cursor.position but please note that you will need to add "windows.form.dll" to your directory, but this will work only on windows standalone build, for mobile,web,apple or linux platform, you need to dig your way trough your self because i dont know ho to do it too.
,
This was a good question and I hope this answer helps, if this answer helps please consider accepting it as answer, this way anyone else who looking for same question can easily find this answer.
Thanks! I've update the question. I don't have to use the mouse cursor. The solution can also use a GameObject that follows the mouse input. Not sure if this changes your answer
that makes things easier :)
here the code for it.
Vector2 PlayerPosition = Vector2.zero;
float radius = 4f;
Vector2 last$$anonymous$$ousePosition = Vector2.zero;
public GameObject fakeCursor; //we defined a fake cursor sprite that follows invisible real cursor
void Start()
{
//Set Cursor to not be visible
Cursor.visible = false;
}
private void Update() {
float distance = Vector2.Distance(Camera.main.ScreenToWorldPoint(Input.mousePosition),PlayerPosition);
if(distance < radius)
{
fakeCursor.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
last$$anonymous$$ousePosition = Input.mousePosition;
}
else if (distance >= radius)
{
fakeCursor.transform.position = Camera.main.ScreenToWorldPoint(last$$anonymous$$ousePosition );
}
}
Answer by Frickinlaser · Dec 06, 2017 at 01:45 PM
I did this recently except for not limiting to the inner circle around the player.
I put the crosshair as a child to the player in the scene hierarchy and a script on it that updates its position.
The localPosition of the crosshair transform is the vector relative to the player object. So just add the input to localPosition and then clamp the magnitude to the radius.
var input = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
var vectorFromParent = transform.localPosition + new Vector3(input.x, input.y, 0) * _speed * Time.deltaTime;
transform.localPosition = Vector2.ClampMagnitude(vectorFromParent, _radius);
I know its been 4 years, but this was insanely helpfull for me! Thanks!
Answer by Yeezyy · Dec 06, 2017 at 11:31 AM
---Not tested--- What you can try is to use a sprite as your mouse and make it follow mouse input only when if distance between mouse position and player position minus inner circle radius in screenspace smaller than the outer circle radius - inner circle radius and greater than inner circle radius
Use Cursor.visible = false ; to hide hardware default cursor Only supports hardware cursors on macOS, Windows and Linux
Your answer
Follow this Question
Related Questions
Huh click and go script working 50% 0 Answers
JavaScript: _player.cursor(280) 1 Answer
Changing cursor over multiple objects 0 Answers
[SOLVED] Mouse cursor disappears in built application 3 Answers
my gun wont follow my mouse 0 Answers