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

[Solved] Physics.OverlapSphere not colliding when inside another Collider

Discussion in 'Physics' started by RedCabinGames, Dec 30, 2017.

  1. RedCabinGames

    RedCabinGames

    Joined:
    May 8, 2016
    Posts:
    7
    Hey,
    I'm trying to make my character teleport a short distance. I want to make sure there are no obstacles in the new location with Physics.OverlapSphere. This works when the colliders actually Overlap, but when the character collider ends up inside another bigger collider, then the collision is not detected. Is this as intended, and if so is there some other way to detect collisions like this?

    I suppose I could combine it with a raycast, but I would really like the player to be able to teleport over objects.
    Explaining image:

    Code:

    Code (CSharp):
    1.  
    2. Vector3 targetPos = ship.transform.position + Vector3.right * teleportDistance;
    3. Collider[] cols = Physics.OverlapSphere(targetPos, Mathf.Max(ship.GetSize().x, ship.GetSize().y), teleportLayerMask, QueryTriggerInteraction.Collide);
    4. if (cols.Length == 0)
    5. {
    6.     Teleport(targetPos);
    7. }
    8.  
    So, can I detect if a position is inside a Collider?
    Thanks!
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    The docs clearly state it should return all colliders overlapping or inside the sphere, so it's probably your code. You are probably going to want to debug how you're calculating the radius and position and debug that visually as well.

    I use the non alloc version of this and my game would explode if it wasn't working well. Perhaps it's a bug in your version of Unity? in any case eliminate the possibility it's your code first.
     
    Mikael-H likes this.
  3. RedCabinGames

    RedCabinGames

    Joined:
    May 8, 2016
    Posts:
    7
    That's what so strange. It should work.
    Since the code works for when they actually overlap I thought there must be something else. I'll try a different Unity version etc.
    Thanks!
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Don't expect mesh colliders to give you good results unless they're a) convex and b) have convex ticked as there's no way they can figure out what is inside - obviously.

    You considered that along with Getsize() providing correct results right?
     
    RedCabinGames likes this.
  5. RedCabinGames

    RedCabinGames

    Joined:
    May 8, 2016
    Posts:
    7
    Yes, I draw a Gizmo with that size at the targetposition. Looks correct.
    Ah, that's probably it, they are actually mesh colliders that are not convex! :/ Will have to investigate that!
    Thank you!

    Edit: Non-convex mesh-collider was the problem! Works perfectly with a sphere collider.
    Is there a way to mark answer as correct or something?
     
    Last edited: Dec 30, 2017
    Mikael-H likes this.
  6. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    This is good advice to add to the docs really. I mean, it's not something you consider first thing when working with Physx / Unity's physics. Glad it fixed your problem!

    You can optionally edit the topic title, but otherwise I don't think there's a marking system for that.
     
    Novack likes this.
  7. shubammishra

    shubammishra

    Joined:
    Aug 29, 2018
    Posts:
    1
    Hi, I am able to get the name of the objects which are there in the collider radius but wants to get the data of the objects which are there in the collider radius. for ex. every object has it's serial number and data. How I can get those things.
    Thanks
     
  8. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Use GetComponent. Serial number doesn't exist so you should learn more about Unity before asking for gibberish.
     
  9. ttmmiizz100

    ttmmiizz100

    Joined:
    Mar 16, 2018
    Posts:
    11
    I know this thread is old, but is there a workaround to this issue? It's not possible to use Convex colliders in my case. I just need to figure out whether a certain point is inside any object.
     
    yeagob likes this.
  10. yeagob

    yeagob

    Joined:
    Jan 27, 2014
    Posts:
    18
    Me too
     
  11. inzade220

    inzade220

    Joined:
    Jan 31, 2020
    Posts:
    1
    I solved that by unchecking the "isKinematic" toggle of my rigidbody!
     
  12. dema3

    dema3

    Joined:
    Sep 20, 2022
    Posts:
    5
    I'm also having an issue with Physics.OverlapSphere. In a webGL build Physics.OverlapSphere is not picking up my generated meshes. But everything works fine in the editor.
    I described my problem a bit more here https://forum.unity.com/threads/iss...erated-mesh-detection-in-unity-build.1433443/. I've tried a few different things, but am pretty stuck with what to do next. Any help would be greatly appreciated. Thanks!