Search Unity

Touch a Sprite

Discussion in '2D' started by DanielX95, Jun 21, 2017.

  1. DanielX95

    DanielX95

    Joined:
    Apr 25, 2017
    Posts:
    24
    Hello,
    I'm new with Unity (but I'm not new with coding), i came from Xcode and SpriteKit.
    I have been building a 2D game with Unity, and i have a simple sprite (a jump icon) and a character. All i want is that if i touch the jump icon (the Sprite) something happen in the game. In C#, how can i say "If i touch that sprite with that name do something". When i was using SpriteKit i was simply write:

    If(node.name == "name") {.do something } inside a touchesBegan method.

    How can i do that in Unity?

    Thanks.
     
  2. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32
    You want to "touch" it with a player or as an input (touch screen) ? :?
     
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    If the sprite has a collider, you can use OnMouseDown:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour
    5. {
    6.     void OnMouseDown()
    7.     {
    8.         Application.LoadLevel("SomeLevel");
    9.     }
    10. }
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html

    I believe this does work for touch, but not multi-touch.
     
    imuniz likes this.
  4. DanielX95

    DanielX95

    Joined:
    Apr 25, 2017
    Posts:
    24
    Hi thanks,

    Yes i want to touch it as an input touch (with finger).

    i could find this code:

    Code (CSharp):
    1. for (var i = 0; i < Input.touchCount; ++i) {
    2.             if (Input.GetTouch(i).phase == TouchPhase.Began) {
    3.                 RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position), Vector2.zero);
    4.                 // RaycastHit2D can be either true or null, but has an implicit conversion to bool, so we can use it like this
    5.                 if(hitInfo)
    6.                 {
    7.                     // Here you can check hitInfo to see which collider has been hit, and act appropriately
    8.                     Debug.Log( hitInfo.transform.gameObject.name );
    9.  
    10.  
    11.                 }
    12.             }
    13.         }
    It works but i've got a small problem: The jump button in the scene is child of the player, so when i touch the button it's like touching the player... How can i make it recognise the button even if it is a player's child?
     
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Well, there's probably a couple of ways to handle that. You can do a RaycastAll, and get multiple results, then check if any of the results were your button.

    Another option would be to define a LayerMask, and use that LayerMask to only check for objects on that Layer.

    So you can create a "UI" layer perhaps, set your button to that layer, then do this:
    Code (CSharp):
    1. public LayerMask touchLayers; // set up in the inspector
    2.  
    3. public void function() {
    4.     for(var i = 0; i < Input.touchCount; ++i) {
    5.         if(Input.GetTouch(i).phase == TouchPhase.Began) {
    6.             Vector3 position = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
    7.             // pass in LayerMask
    8.             RaycastHit2D hitInfo = Physics2D.Raycast(position, Vector2.zero, Mathf.Infinity, touchLayers);
    9.             // RaycastHit2D can be either true or null, but has an implicit conversion to bool, so we can use it like this
    10.             if(hitInfo) {
    11.                 // Here you can check hitInfo to see which collider has been hit, and act appropriately
    12.                 Debug.Log(hitInfo.transform.gameObject.name);
    13.             }
    14.         }
    15.     }
    16. }
    Another way would be to use collider2D.OverlapPoint() to check that specific collider for the mouse position overlap.
     
  6. DanielX95

    DanielX95

    Joined:
    Apr 25, 2017
    Posts:
    24
    Thanks,
    Okay i did the layers and it works, but the problem is the same: The button is child of player, so in console appears "Player" and not the real name of the button. How can i fix that?
     
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    If the layers are set up correctly, you would never detect the "Player" gameObject. The "hitInfo" would have a reference to the child object only.