|
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 Which defines where my mouse is and 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. This time Haha comes up but not lol.
(comments are locked)
|
|
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.
Now, you can translate this offset into polar coordinates, to get an angle and a radius-
Now that you have the angle, you can use that to determine what quadrant the mouse is in! 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)
|

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.
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.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).
Thank you for your time/help works perfect now.
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.