Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

IsTouching() not working

Discussion in 'Scripting' started by Scyleung, Apr 22, 2016.

Thread Status:
Not open for further replies.
  1. Scyleung

    Scyleung

    Joined:
    Nov 29, 2013
    Posts:
    3
    I am trying to see if two GameObjects with a Box Collider 2D is touching/overlapping. Since I only need to worry about this when the user click, I decided that using OnCollisionEnters2D or other function based checking is not necessary, Hence I am trying to use IsTouching or IsTouchingLayers.

    I version of the code I tried with IsTouching is:
    Code (JavaScript):
    1. var towers = GameObject.FindGameObjectsWithTag("Tower");
    2. for (var tower : GameObject in towers) {
    3.     if (GetComponent(Collider2D).IsTouching(tower.GetComponent(Collider2D))) {
    4.         Debug.Log("Touch");
    5.     }
    6. }
    And I tried with IsTouchingLayers:
    Code (JavaScript):
    1. var layer : LayerMask = LayerMask.GetMask("Towers");
    2. if (GetComponent(Collider2D).IsTouchingLayers(layer)) {
    3.     Debug.Log("Touch");
    4. }
    The objects I am trying to see if touching is tagged with "Tower" and in the layer "Towers". However, both code never returned true even though they are clearly overlapping. I also tried switching "Is Trigger" on and off both the objects with no luck...
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    there are caveats on the API pages for those about how/when the value is updated being based off the "last physics step"... so I'm guessing timing might be involved or is there time between the last position update and the click?
     
  3. Scyleung

    Scyleung

    Joined:
    Nov 29, 2013
    Posts:
    3
    I am not sure about the timing of the physics step, but none of the objects were moving. So unless physics isn't running at all... or take over like 10 seconds to update? I don't really think timing could be an issue.
    Tower objects never moves, cause you know, they are towers, and the object this script is running on is linked to the mouse position.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    default is 50 times a second...

    Is there a rigidbody attached to something? (think that's still a requirement in 2d, certainly is with 3d)
     
    deadlydog likes this.
  5. Scyleung

    Scyleung

    Joined:
    Nov 29, 2013
    Posts:
    3
    Thanks, that was the problem, I needed a rigidbody attached, seem weird to require a rigidbody for something like this.
     
    hopetolive likes this.
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the physics engine deals with interaction within rigidbodies and between rigidbodies and colliders; no rigidbody involved, the physic engine doesn't know anything happened.
     
  7. cyanjamgames_thomas

    cyanjamgames_thomas

    Joined:
    Oct 15, 2018
    Posts:
    18
    My problem is that IsTouching returns false even when touching my object.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class appleSpawn : MonoBehaviour {
    7.  
    8.     public int appleID;
    9.     public int colID;
    10.  
    11.     public System.Random generator = new System.Random();
    12.  
    13.     public GameObject redApple;
    14.     public GameObject goldApple;
    15.     public GameObject poisonApple;
    16.  
    17.     public GameObject redAppleInGame;
    18.     public GameObject goldAppleInGame;
    19.     public GameObject poisonAppleInGame;
    20.  
    21.     public GameObject playerSprite;
    22.  
    23.     public Collider2D playerCollision;
    24.     public Collider2D redAppleCollision;
    25.     public Collider2D goldenAppleCollision;
    26.     public Collider2D poisonousAppleCollision;
    27.  
    28.  
    29.     public int spawnLeastWait = 3;
    30.     public int spawnMostWait = 8;
    31.  
    32.     public Vector3 spawnPOS;
    33.  
    34.     void Start()
    35.     {
    36.         StartCoroutine(Spawner());
    37.     }
    38.  
    39.     void Update()
    40.     {
    41.         if (appleID == 0)
    42.         {
    43.             redAppleInGame.transform.Translate(0, -10, 0);
    44.             if (redAppleInGame.transform.position.y < -9409)
    45.             {
    46.                 Destroy(redAppleInGame);
    47.             }
    48.             else if (redAppleCollision.IsTouching(playerCollision))
    49.             {
    50.                 Player.Player.RedApple(Player.Player.points, Player.Player.speed);
    51.                 Debug.Log("Touching");
    52.                 Destroy(redAppleInGame);
    53.             }
    54.         }
    55.  
    56.         if (appleID == 1)
    57.         {
    58.             goldAppleInGame.transform.Translate(0, -10, 0);
    59.             if (goldAppleInGame.transform.position.y < -9409)
    60.             {
    61.                 Destroy(goldAppleInGame);
    62.             }
    63.             else if (goldenAppleCollision.IsTouching(playerCollision))
    64.             {
    65.                 Player.Player.GoldenApple(Player.Player.points, Player.Player.speed, Player.Player.health);
    66.                 Debug.Log("Touching");
    67.                 Destroy(goldAppleInGame);
    68.             }
    69.         }
    70.  
    71.         if (appleID == 2)
    72.         {
    73.             poisonAppleInGame.transform.Translate(0, -10, 0);
    74.             if (poisonAppleInGame.transform.position.y < -9409)
    75.             {
    76.                 Destroy(poisonAppleInGame);
    77.             }
    78.             else if (poisonousAppleCollision.IsTouching(playerCollision))
    79.             {
    80.                 Player.Player.PoisonApple(Player.Player.points, Player.Player.speed, Player.Player.health);
    81.                 Debug.Log("Touching");
    82.                 Destroy(poisonAppleInGame);
    83.             }
    84.         }
    85.     }
    86.  
    87.     IEnumerator Spawner() {
    88.         appleID = generator.Next(0, 2);
    89.         colID = generator.Next(0, 2);
    90.  
    91.         if (colID == 0)
    92.         {
    93.             spawnPOS = new Vector3(playerMovement.col1.x, 0, 2);
    94.         }
    95.  
    96.         else if (colID == 1)
    97.         {
    98.             spawnPOS = new Vector3(playerMovement.col2.x, 0, 2);
    99.         }
    100.  
    101.         else if (colID == 2)
    102.         {
    103.             spawnPOS = new Vector3(playerMovement.col3.x, 0, 2);
    104.         }
    105.  
    106.         yield return new WaitForSeconds(2);
    107.  
    108.         if (appleID == 0)
    109.         {
    110.             redAppleInGame = Instantiate(redApple, spawnPOS, Quaternion.identity);
    111.         }
    112.  
    113.         if (appleID == 1)
    114.         {
    115.             goldAppleInGame = Instantiate(goldApple, spawnPOS, Quaternion.identity);
    116.         }
    117.  
    118.         if (appleID == 2)
    119.         {
    120.             poisonAppleInGame = Instantiate(poisonApple, spawnPOS, Quaternion.identity);
    121.            
    122.         }
    123.  
    124.     }
    125. }
    126.  
     
  8. ChalothornYns

    ChalothornYns

    Joined:
    Oct 5, 2021
    Posts:
    1
    To make IsTouching() works you need to
    1. attach Rigidbody2D to the collider that goes in to the function
    2. check Is Trigger of that collider to true
     
    Last edited: Jan 25, 2022
    JustJunuh, deadlydog and ptuananh196 like this.
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,374
    Note that there is no requirement for it being attached to a Rigidbody2D nor does it need to be a trigger.

    Please note the age of posts. This was posted nearly 6 years ago with the last reply being several years ago.

    See Necro Posting in our Community Code of Conduct.
     
Thread Status:
Not open for further replies.