Comparing finger input position to object position [Android][C#]

So i’m trying to create a script that checks if the user finger input is above or below the game object to perform a certain task.

for (int i = 0; i < Input.touchCount; ++i) {
            Touch touch = Input.GetTouch(i);

                if (touch.position.y > transform.position.y) {
                //This is the part that gets called no matter where I click on the screen
                    transform.Translate (Vector2.right * -Speed * Time.deltaTime);
                }

                if (touch.position.y <= transform.position.y) {
                    transform.Translate (Vector2.right * Speed * Time.deltaTime);
                }
        }

This code used to work when I simply checked if the player is clicking above or below the center of the screen, however when trying to compare it to a world object the first if statement is called every time no matter where I click.

I’m not sure what the problem is but I’d assume it has something to do with comparing touch coordinates to world object coordinates.

I’ve been thinking about using ScreenToWorldPoint but as both are vector2 coordinates i’m not sure if it’s necessary.

(NOTE: this script is attached to the player)

Any help is appreciated!

That’s exactly it.

The touch.position.y value is going to be between 0 and the screen height, so it wouldn’t have any resemblance to the gameobject’s transform y value, even if they’re both Vector2 coordinates.

You need to use WorldToScreenPoint. Compare your touch position to the WorldToScreenPoint position of your object.
Have a look at this link: Unity - Scripting API: Camera.WorldToScreenPoint

Alright going to try that soon, thank you for the responses!

This is how I determine the exact point.

var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var distance_plane : float;
var zero_plane : Plane = new Plane(Vector3.up, Vector3.zero);
       
    if(zero_plane.Raycast(ray, distance_plane))
    {
        var ray_point : Vector3 = ray.GetPoint(distance_plane);
    }

Try This…

if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
if (touch.position.y > transform.position.y)
{
var pos = Camera.main.ScreenToWorldPoint(touch.position);
if (pos.y > transform.position.y)
{
// UP
}
else if (pos.x < transform.position.x)
{
//Down
}
}
}
}

This Code Working Perfectly… Thank You