Search Unity

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

Discussion in 'Scripting' started by GNGification, Nov 28, 2014.

  1. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    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.

    Code (CSharp):
    1.  
    2. for (int i = 0; i < Input.touchCount; ++i) {
    3.             Touch touch = Input.GetTouch(i);
    4.  
    5.                 if (touch.position.y > transform.position.y) {
    6.                 //This is the part that gets called no matter where I click on the screen
    7.                     transform.Translate (Vector2.right * -Speed * Time.deltaTime);
    8.                 }
    9.  
    10.                 if (touch.position.y <= transform.position.y) {
    11.                     transform.Translate (Vector2.right * Speed * Time.deltaTime);
    12.                 }
    13.         }
    14.  
    15.  
    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!
     
  2. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    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.
     
  3. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
  4. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    Alright going to try that soon, thank you for the responses!
     
  5. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    This is how I determine the exact point.

    Code (csharp):
    1.  
    2. var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    3. var distance_plane : float;
    4. var zero_plane : Plane = new Plane(Vector3.up, Vector3.zero);
    5.        
    6.     if(zero_plane.Raycast(ray, distance_plane))
    7.     {
    8.         var ray_point : Vector3 = ray.GetPoint(distance_plane);
    9.     }
    10.  
    11.  
     
  6. Dharmesh_Fulmali

    Dharmesh_Fulmali

    Joined:
    Sep 18, 2018
    Posts:
    1
    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
    }
    }
    }
    }
     
  7. PLXI_GAMES

    PLXI_GAMES

    Joined:
    Feb 25, 2021
    Posts:
    1
    This Code Working Perfectly... Thank You