Problem with using negative and positive mouse coordinates.

Background the problem

Hi guys at the moment I’m trying to make a word search game.

The game’s layout will be 10 x 10 grid of letter squares. When the player clicks on any letter squares the player can move the mouse in any direction and a straight red line from the centre of letter square to the mouse, regardless of the mouse degrees. For instance if the mouse is at 87 degrees from that square the line would be a straight 90 degrees Right line and the same for any direction.

To achieve this I turn the X and Y, transform coordinates of the mouse into Veriables. which is picked up by “straight red line” game object script. which interprets the mouses X and Y axis Veriables if the X axis is Greater than Y axis then draws line straight Right, by ignoring the Y axis (See code below).

// X Y Positive, Top Right Of “letter squares” Game object

if (getinfo.GetMX > 0 && getinfo.GetMY > 0) // script is triggered when the mouse only in the top right space of the “letter square” Game object
{
if (getinfo.GetMX > getinfo.GetMY) // If the X axis is Greater than Y axis draws line straight Right by ignoring the Y axis.
{
transform.position = new Vector3 (getinfo.GetMX, 0, 0);
}

if (getinfo.GetMY > getinfo.GetMX) // If the Y axis is Greater than X axis draws line straight up by ignoring the X axis.
{
transform.position = new Vector3 (0, getinfo.GetMY, 0);
}

if (getinfo.GetMX > 1 && getinfo.GetMY > 1) // If The mouse is in a roughly 45° line from the letter square. Only the X axis is used in the new vector 3 to give a straight diagonal line.
{
transform.position = new Vector3 (getinfo.GetMX, getinfo.GetMX, 0);
}
}

Problem

This system works very well when I’m just dealing with positive numbers or negative numbers like top right of the letter square or bottom left of the letter square. The problem I have is dealing with positive and negative numbers, like top left of letter square when the X axis is negative and the Y axis is positive. 1 Y axis is always going to be bigger than -10 X axis. The script keeps drawing a straight red line up , but never left.

Also how would I approach a diagonal straight red line when X axis negative and the Y axis is positive.

Let’s say GetMX is equal to -10 and GetMY is equal to 1

if (getinfo.GetMX > getinfo.GetMY)
{
transform.position = new Vector3 (getinfo.GetMX, 0, 0);
}

if (getinfo.GetMY > getinfo.GetMX)
{
transform.position = new Vector3 (0, getinfo.GetMY, 0);
}

In both these instances only a line straight red line up will be produced.

Any help would be appreciatedstrong text

Hi Hexagonius

Thanks for telling me about Mathf.Abs() code,

I used an if statement to work out if the mouse was in a positive and negative X Y space Top left or bottom right of the letter square. I then converted the mouse X, Y axis using Mathf.Abs() into another variable. Both X, Y axis are now positive, I used another if statement to check, If X was greater than Y then used X axis of the mouse to draw the straight line.

// X Y Positive, Top Left Of “letter squares” Game object
if (getinfo.GetMX < 0 && getinfo.GetMY > 0) // script is triggered when the mouse only in the top right space of the “letter square” Game object
{
//Debug.Log (You are here!! Top Left x- Y+" );

		overrideX = Mathf.Abs (getinfo.GetMX); // Convert the variable to the absolute changing from negative to positive
		overrideY = Mathf.Abs (getinfo.GetMY); // Convert the variable to the absolute changing from negative to positive

		if (overrideX > overrideY) // If the X axis is Greater than Y axis draws line straight Right by ignoring the Y axis.
			{
			//Debug.Log ("Top Left x- Y+ X"  );
				transform.position = new Vector3 (getinfo.GetMX, 0, 0);
			}
		
		if (overrideY > overrideX) // If the Y axis is Greater than X axis draws line straight up by ignoring the X axis.
			{
			//Debug.Log ("Top Left x- Y+ Y" );
				transform.position = new Vector3 (0, getinfo.GetMY, 0);
			}

Thank you very much for all your help