Hi,
I’m trying to use what I’ve learned so far to find (and display for testing) the position of my mouse so I can use that position to trigger certain actions of my transform. I’m not sure what all of the math involved is to accomplish this fully.
What I have so far works, for the intended purpose, but it’s not exactly what I’m looking for. I’m trying to “quadrant” the screen to 4 sections with the center being zero. To the left of Xcenter I need zero (xCenter) to -1 (Left edge if screen), to the right of Xcenter, equals positive 1 (Right edge of screen). The same for Y except above Ycenter, positive 1(Top edge of screen) and below Ycenter -1(Bottom edge of screen).
As close as I can get is zero to 1 using this:
using UnityEngine;
using System.Collections;
public class MousePositionTest : MonoBehaviour
{
// These help find the center of the screen. It's not exactly what I'm trying to find, but works for what I need it for.
// I'm trying to get the center to 0 and then -1 to the left of xcenter and positive 1 to the right of xcenter, also
// -1 below ycenter and positive 1 above ycenter. The purpose is to allow particular "action" of the ship depending
// on the mouse screen position when the mouse button is pressed for Yaw.
private float xCenter = (Screen.width / 2);
private float yCenter = (Screen.height / 2);
private Vector3 mousePos;
private Vector2 mouseDeltaPos;
void Update()
{
mousePos = Input.mousePosition;
mouseDeltaPos = new Vector2((mousePos.x / xCenter) / 2, (mousePos.y / yCenter) / 2);
}
void FixedUpdate()
{
if(Input.GetMouseButton(1) mouseDeltaPos.x > .55f)
{
//do something
}
else if(Input.GetMouseButton(1) mouseDeltaPos.x < .45f)
{
//do something
}
else
return;
}
public void OnGUI()
{
GUI.Label(new Rect(10, 10, 150,50), "X_Position: " + mouseDeltaPos.x);
GUI.Label(new Rect(10, 30, 150, 50), "Y_Position: " + mouseDeltaPos.y);
}
}
I have to use .5 as my center, where mouseDeltaPos.x = 0 equals the left side of screen, mouseDeltaPos.x = 1 equals the right side of the screen.
How do I get?
mouseDeltaPos.x = -1 equal left side of screen
mouseDelta.x = 0 center
mouseDeltaPos.x = 1 equal the right side of screen
Doing a google search for mouse screen delta, mouse position, how to (insert any of what I’ve asked) yields no answers to anything I have questions about.
I’ve spent hours looking and can’t find anything related to my questions.
Any help or insight would be appreciated greatly.