IsTouching() not working

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:

var towers = GameObject.FindGameObjectsWithTag("Tower");
for (var tower : GameObject in towers) {
    if (GetComponent(Collider2D).IsTouching(tower.GetComponent(Collider2D))) {
        Debug.Log("Touch");
    }
}

And I tried with IsTouchingLayers:

var layer : LayerMask = LayerMask.GetMask("Towers");
if (GetComponent(Collider2D).IsTouchingLayers(layer)) {
    Debug.Log("Touch");
}

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…

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?

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.

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)

1 Like

Thanks, that was the problem, I needed a rigidbody attached, seem weird to require a rigidbody for something like this.

2 Likes

the physics engine deals with interaction within rigidbodies and between rigidbodies and colliders; no rigidbody involved, the physic engine doesn’t know anything happened.

My problem is that IsTouching returns false even when touching my object.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class appleSpawn : MonoBehaviour {

    public int appleID;
    public int colID;

    public System.Random generator = new System.Random();

    public GameObject redApple;
    public GameObject goldApple;
    public GameObject poisonApple;

    public GameObject redAppleInGame;
    public GameObject goldAppleInGame;
    public GameObject poisonAppleInGame;

    public GameObject playerSprite;

    public Collider2D playerCollision;
    public Collider2D redAppleCollision;
    public Collider2D goldenAppleCollision;
    public Collider2D poisonousAppleCollision;


    public int spawnLeastWait = 3;
    public int spawnMostWait = 8;

    public Vector3 spawnPOS;

    void Start()
    {
        StartCoroutine(Spawner());
    }

    void Update()
    {
        if (appleID == 0)
        {
            redAppleInGame.transform.Translate(0, -10, 0);
            if (redAppleInGame.transform.position.y < -9409)
            {
                Destroy(redAppleInGame);
            }
            else if (redAppleCollision.IsTouching(playerCollision))
            {
                Player.Player.RedApple(Player.Player.points, Player.Player.speed);
                Debug.Log("Touching");
                Destroy(redAppleInGame);
            }
        }

        if (appleID == 1)
        {
            goldAppleInGame.transform.Translate(0, -10, 0);
            if (goldAppleInGame.transform.position.y < -9409)
            {
                Destroy(goldAppleInGame);
            }
            else if (goldenAppleCollision.IsTouching(playerCollision))
            {
                Player.Player.GoldenApple(Player.Player.points, Player.Player.speed, Player.Player.health);
                Debug.Log("Touching");
                Destroy(goldAppleInGame);
            }
        }

        if (appleID == 2)
        {
            poisonAppleInGame.transform.Translate(0, -10, 0);
            if (poisonAppleInGame.transform.position.y < -9409)
            {
                Destroy(poisonAppleInGame);
            }
            else if (poisonousAppleCollision.IsTouching(playerCollision))
            {
                Player.Player.PoisonApple(Player.Player.points, Player.Player.speed, Player.Player.health);
                Debug.Log("Touching");
                Destroy(poisonAppleInGame);
            }
        }
    }

    IEnumerator Spawner() {
        appleID = generator.Next(0, 2);
        colID = generator.Next(0, 2);

        if (colID == 0)
        {
            spawnPOS = new Vector3(playerMovement.col1.x, 0, 2);
        }

        else if (colID == 1)
        {
            spawnPOS = new Vector3(playerMovement.col2.x, 0, 2);
        }

        else if (colID == 2)
        {
            spawnPOS = new Vector3(playerMovement.col3.x, 0, 2);
        }

        yield return new WaitForSeconds(2);

        if (appleID == 0)
        {
            redAppleInGame = Instantiate(redApple, spawnPOS, Quaternion.identity);
        }

        if (appleID == 1)
        {
            goldAppleInGame = Instantiate(goldApple, spawnPOS, Quaternion.identity);
        }

        if (appleID == 2)
        {
            poisonAppleInGame = Instantiate(poisonApple, spawnPOS, Quaternion.identity);
           
        }

    }
}

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
3 Likes

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 .