Search Unity

[Help] Bounding Box Collision with Tileset (Without Rigidbodies) (Wrong section rip, moved to 2D)

Discussion in 'Physics' started by MossCairn, Oct 1, 2019.

  1. MossCairn

    MossCairn

    Joined:
    Mar 14, 2019
    Posts:
    10
    Alright, so I’m making a lot of progress with my collisions in unity, but I can’t get tilemap collision detection working quite yet. I have a grid with a composite collider, and attached to that is a tilemap with a tilemap collider. The player uses a bounding box. I’m trying to make it so that if the player’s bounding box intersects a tile, it will return true, and if it doesn't it will return false. However, my code currently gets the bounds of the entire tilemap collider, and after digging through the documentation I'm not sure how to only detect bounding box intersections with actual solid tiles.
    I should note that I'm only trying to get a true/false for if the player's bounding box is intersecting another bounding box, and possibly what objects I'm overlapping. I would also really like to avoid using Rigidbodies, because I can handle my own collisions and movement and even having kinematic rigidbodies on everything is something I would prefer to avoid.

    Here's the collision code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class collideAt : MonoBehaviour
    6. {
    7.     //eventually going to replace this with a collection of all colliders in the scene or something for performance
    8.     public GameObject self, target;
    9.     Collider2D selfCollider, targetCollider;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         //if we have a collider (just to be safe lol)
    15.         if (self != null)
    16.         {
    17.             selfCollider = self.GetComponent<Collider2D>();
    18.         }
    19.         else
    20.         {
    21.             Debug.Log("I don't have a collider");
    22.         }
    23.  
    24.         //if the target has a collider
    25.         if (self != null)
    26.         {
    27.             targetCollider = target.GetComponent<Collider2D>();
    28.         }
    29.         else
    30.         {
    31.             Debug.Log("Target has no collider");
    32.         }
    33.  
    34.  
    35.  
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update()
    40.     {
    41.         if (selfCollider.bounds.Intersects(targetCollider.bounds))
    42.         {
    43.             Debug.Log("Bounds intersecting");
    44.         }
    45.     }
    46.  
    47.     //private void OnTriggerEnter(Collider other)
    48.     //{
    49.     //    Debug.Log("Bounds intersecting");
    50.     //}
    51. }
    52.  
     

    Attached Files:

  2. MossCairn

    MossCairn

    Joined:
    Mar 14, 2019
    Posts:
    10
    Reposting in 2d, physics isn't really the right section for this one :/