Search Unity

How to get GameObject in touch position

Discussion in '2D' started by Francko, Dec 8, 2019.

  1. Francko

    Francko

    Joined:
    Apr 10, 2015
    Posts:
    1
    Hi everyone.
    So, I'm making a 2D mobile game. In order to move your player character you need to touch on a specific game object, let's call them "squares". I'm making a few squares with a tag (i.e "target").
    What I need is a way to "capture" the game object in the touch position (
    touchPosition = Camera.main.ScreenToWorldPoint (touch.position); ) and check if tag equals to target.

    How can I achieve this?

    Thanks and sorry for my english!
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Code (CSharp):
    1. OnCollisionEnter2D(Collision2D col)
    2. {
    3. if(col.gameObject.tag == "Your Tag Here")
    4. }
    or you could

    Code (CSharp):
    1. private void Update()
    2. {
    3. if(touchPosition == gameObject.CompareTag("Your Tag Here"))
    4. }
    I think that might work.