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.