Determining the mouse position against the center of the screen.

Hi there,

Brand new to Unity, and getting started on the Javascript, what I’m trying to do is get the position of the mouse relative to the center of the screen, as well as the cursors distance from the center of the screen.

I don’t need to have the code handfed to me (although that would work too) I just need to know logically how i’m going to accomplish this.

Basically what I want is to determine which Quadrant of the screen the cursor is in, as if there was a Cartesian plane drawn on screen with the center of the screen at (0 , 0) so that I can handle each case separately, the next part I need to do is determine the cursors distance from the center of the screen to modify the effects of each case.

So far I understand that I can pull the cursors position however (0, 0) is the bottom left of the screen, I also saw that there are the Screen.Height and Screen.Width variables, I figure that I can get an average of those to determine the center and then work from there, however I’m not sure exactly how to do this in Javascript.

Thank you for any help you can give!

Take half the screen width and that will be your horizontal “center”.

Take half the screen height and that will be your vertical “center”.

Then you can work from that variable, subtracting from it gives you the “negative” parts of the grid, adding to it gives you the “posiitve” parts. Basically, you can make a couple of variables like “halfScrW” and “halfScrH” to be your “0,0” coordinates as you describe, and it will always be the center regardless of resolution. The bounds would be the same as these, but in each direction along the X/Y axes.

Anything you need to calculate can be done by taking the current cursor location and checking it against these conceptual “center 0, 0” values.

Hope this helps clarify for you conceptually :).

Oh you are amazing, sometimes my head gets a little clogged and nothing helps more than a clear, concise explanation.

Way easier than I anticipated, I’m excited to get off work and get this done!

Many thanks!

private var screenCenter : Vector2;

function Awake () {
    screenCenter = Vector2(Screen.width/2, Screen.height/2);
}

That should give you a starting point I hope. Note that Unity 3 has a bug where this won’t work if you use maximize on play…the screen width and height are not correct for the first two or three frames. You can work around that in code, but it’s probably best if you maximize the game view yourself using space before hitting play.

–Eric

1 Like

Well, not too familiar with Javascript(I use C#), but I do know the Unity API.

So first step would be to figure out when you want to do it. You have a lot of options (see Unity - Scripting API: MonoBehaviour for available functions in a regular script), but let’s choose Update() as it’s common.

Steap 1 is make a new script, name it and attach it onto a gameobject. because it’s not really location based, it can either go on the camera or on an empty gameobject put anywhere. Just drag the script file onto the Camera in the hierarchy view.

so in code you should see:

function Start()
{

}

function Update()
{

}

Update is called every frame, Start is called when that object is created, or at the start of the level.

Your test is easy because all the parts you need are static and can be gotten from anywhere. You don’t need to have a Screen object to get the height or width.

But you don’t actually want the width or height but rather half that. So you need to get a Vector2 (something that stores 2 floats values/decimal numbers) that is Width and height divided by two.

So above function Start() you create a Vector2:

var halfScreen : Vector2;

Makes a Vector2 class variable. Unity - Scripting API: Vector2 for more info on those.

now you need to fill it with stuff. The Screen size shouldn’t change and it’s expensive to constantly check it, so it’s going to go into the Start function. You can use this syntax to set the x and y parts of the Vector2:

halfScreen = Vector2(Screen.width/2.0, Screen.height/2.0);

The order is x,y so it’s width,height.

halfScreen is used to offset the 0,0 bottom left position into a center position like this:

var positionCentered : Vector2 = Input.mousePosition - halfScreen;

This will shift all values downwards by half the screen.
Now it’s easy to figure out quadrants by just detecting whether it’s a positive or negative x,y value (x for horizontal, y for vertical)

Using the Mathf class you can do that quickly and cleanly.

or more specifically:

So you can get the quadrant as:
var quadrant : Vector2 = Vector2(Mathf.Sign(positionCentered.x), Mathf.Sign(positionCentered.y));
Basically that will give you 1,-1 or 1,1, or 0,1 or 0,-1 and give an easy value for finding coordinates.

You can check that value with the print function. after figuring out quadrant you can do ‘print(quadrant);’ and it will write it to Unity’s console.

I’m not sure your experience with other languages or programming in general but I hope this helped without feeling too simple. I didn’t provide straight code on purpose but set it up so it can all be built from these lines + what Unity gives you in a new script.

Alot of this relies on Unity’s built in functions so it’s easy, and while learning I suggest you always have a copy of Unity - Scripting API: open. It’s a lifesaver and will always speed up and help you find what you need and how to use it.

Edit: turns out my super long teacher post over simplified and took too long.

Oh by all means, the more information the better, I’ll post a link to my little demo once it’s finished!

Thanks for the help everyone you’ve been great!

Hey everyone I’ve got the part that I was stuck on now and it works!

Not exactly the way I want yet but I have some ideas as to how to fix it up, take a look!

http://panickmedia.com/~dailyBuild/WebPlayer/WebPlayer.html

Press W and E to decrease and increase thrust respectively, ( this still isn’t behaving 100% but i’m still doing some tweaking)
WSAD will give you a strafing movement (my version of maneuvering thrusters)
and of course the position of the mouse determines the rotation of the ship (flight simulator style)

Pardon the graphics haha, but dag nabbit this is aboiut coding!

The problem is that turning doesn’t quite work but I think the solution is to make the ship pitch the ship downwards as you roll more to the left and right.

Any input on my solution (which I’m going to start on now) or any other problems / solutions with my controls would be great!