Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Validate Object Placement

Discussion in 'Physics' started by LukasO, Dec 17, 2014.

  1. LukasO

    LukasO

    Joined:
    May 23, 2013
    Posts:
    115
    Hi,

    Just wanted to ask whether it's possible to check for collisions between objects at runtime? At the moment I'm placing objects and I'm doing bounding box checks to see whether the object I'm placing overlaps any other existing object and if it does, I discard the new object and try to place another. This has worked fine.

    With more advanced room shapes, bounding shapes are now much harder to set up. I would instead like to do a similar test using Mesh Colliders but I notice there's not a way in script to check if an object is colliding as soon as its created. I've thought about using trigger callbacks but unless I'm mistaken a frame needs to before that callback will be used and I'm really trying to generate a lot of objects at once rather than over a period of time so this isn't desirable.

    So just wondered if there are any other suggestions?

    Thanks,

    Lukas
     
  2. polytropoi

    polytropoi

    Joined:
    Aug 16, 2006
    Posts:
    681
    Before instantiating your object, you can put an empty at your target location and do a spherecast from it, so that the radius is a bit bigger than the object to be instantiated, then analyze the return to see if there would be collisions at the sampled location.
     
  3. LukasO

    LukasO

    Joined:
    May 23, 2013
    Posts:
    115
    That's not suitable as there's not enough precision. I think my solution will be to split an object into a multiple colliders and use the bounds from each box collider to test if the object overlaps. With multiple box colliders, they can be used to represent smaller spaces and form the shape required all together. It would potentially work with the methodology you mentioned as well but I want as much stored with the prefab as possible.