- Home /
Check if clicked position is on left, right, up or down from an gameObject
I get a clicked position and I need to figure out which coordinates it is in.
I've tried by angle but I guess it isn't the best way to reach it because angle can be very variable and imprecise.
The code so far:
if (Input.mousePresent && Input.GetMouseButtonDown(0))
{
Vector3 clickedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float angle = Mathf.Round(Vector3.Angle(transform.position, clickedPosition));
Debug.Log("Angle: " + angle);
}
I don't know if it was implemented natively on Unity somehow or I have to implement something different of angle.
Thanks in advance
Answer by robertbu · Jul 06, 2014 at 11:56 PM
One solution is to convert the point into local coordinates.
if (Input.mousePresent && Input.GetMouseButtonDown(0))
{
Vector3 clickedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
clickedPosition = transform.InverseTransformPoint(clickedPosition);
if (clickedPosition.z > 0)
Debug.Log("Front");
else
Debug.Log("Back");
if (clickedPosition.x > 0)
Debug.Log("Right");
else
Debug.Log("Left");
if (clickedPosition.y > 0)
Debug.Log("Above");
else
Debug.Log("Below");
}
Thank you so much for reply, this is indeed works pretty good, unfortunately what I need is something little different but like I said it, this answer is correct, when I adjust the code to fit what I'm looking for I post back here again.
Your answer
Follow this Question
Related Questions
Find side ways from one to another point 1 Answer
How to rotate back and forth on the X axis? 0 Answers
Why isn't my script to move particles working? 0 Answers
MAKE A LEVITATING PLATFORM REACT TO A PLAYER'S JUMP? 1 Answer
Adding force to a bullet 3 Answers