x


Mouse Placement in Screen issue / Need Help.

I am unsure on the topic so I figured I should ask the helpful folk at Unity Answers. What I want to do is make an if statement which asks whether the mouse position is in a certain area of the screen.

I am completely unsure on how to do this So I have

   mousePlacement = Input.mousePosition;

Which defines where my mouse is and

screenCentre = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);

which defines where the screen centre is. So what I want to do if possible is an iff statement asking whether the mousePlacement is in a certain area of the screen. Im unsure how to do this or if it is possible. For instance is the mouse somewhere in the left centre, the top left, the bottom left, middle top, middle centre, middle bottom, right top, right middle, right bottom. If you get me :/

Help is much appreciated.

Thank you for your time as always :)

EDIT: Update

I did what you put and got errors so I edited it to my code and it worked. But the only area I could get going was the one that you put down angle > 0 && angle < 90 im debugging it by print messages is there anyway you could define which angles of the screen are which parts. Notice that I changed it away from using the x axis as my screen look down to x and z.

screenCentre = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);
Vector3 offset = Input.mousePosition - screenCentre;
    float angle = Mathf.Atan2(offset.z, offset.x) *Mathf.Rad2Deg;
    float radius = offset.magnitude;
    if(angle > 0 && angle < 90){
    // The mouse must be in the top-right side of the screen!
       print("lol");
       }
    if(angle > 90 && angle < 180){
       print("haha");
       }

This time Haha comes up but not lol.

more ▼

asked Jan 17 '12 at 06:19 AM

doomprodigy gravatar image

doomprodigy
557 22 26 31

Where in that code do you take the mouse position into account? This only seems to be counting the screen centre! Where did errors come up? Tell me so I can edit my answer to make it more correct.

Jan 17 '12 at 07:54 AM syclamoth

Ah shit I see what you did. The errors I got with you way is The call is ambiguous between the following methods or properties: UnityEngine.Vector2.operator -(UnityEngine.Vector2, UnityEngine.Vector2)' andUnityEngine.Vector3.operator -(UnityEngine.Vector3, UnityEngine.Vector3)' So I changed my edit again cause I see what I did wrong to incorporate the mouse position (Edited scripts again. Now what happens is that I get haha but I do not get lol.

Jan 17 '12 at 08:03 AM doomprodigy

Now the problem is that 'Input.MousePosition' uses the x and y coordinates, and your screenCentre variable uses the x and z coordinates! Just use the actual code I posted, you're just confusing things by trying to make them into Vector3s. If anything, you should be trying to make every variable there a Vector2, so that the maths work properly. (I've edited my answer to fix the error you reported, btw).

Jan 17 '12 at 08:09 AM syclamoth

Thank you for your time/help works perfect now.

Jan 17 '12 at 08:13 AM doomprodigy

Which shape are your areas supposed to have? You only need polar coords, if they are supposed to be circular around the screen center.

If you want to have rectangular areas, then you can use the screen coords directly.

Jan 17 '12 at 08:21 AM senad
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Well, already with what you have you can do a pretty good job of it!

First off, subtract the middle of the screen from the mouse position to get an 'offset' value.

Vector2 offset = (Vector2)Input.mousePosition - new Vector2(Screen.width / 2, Screen.height / 2);

Now, you can translate this offset into polar coordinates, to get an angle and a radius-

float angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
float radius = offset.magnitude;

Now that you have the angle, you can use that to determine what quadrant the mouse is in!

if(angle > 0 && angle < 90)
{
    // The mouse must be in the top-right side of the screen!
}
more ▼

answered Jan 17 '12 at 07:22 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

Updated Question with response :).

Jan 17 '12 at 07:42 AM doomprodigy

Thank you :)

Jan 17 '12 at 08:13 AM doomprodigy

Removed accepted status as I have encountered a problem. It will not read for bottom left and right. using >180 and < 270 also with >270 and < 360. Any clue?

Jan 17 '12 at 08:17 AM doomprodigy

Ignore that problem solved.

Jan 17 '12 at 08:18 AM doomprodigy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2
x1
x1

asked: Jan 17 '12 at 06:19 AM

Seen: 531 times

Last Updated: Jan 17 '12 at 08:26 AM

Related Questions