Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Check for Collision before Gameobject Spawn

Discussion in 'Scripting' started by dara_mh, May 14, 2019.

  1. dara_mh

    dara_mh

    Joined:
    Mar 20, 2019
    Posts:
    3
    Hello,

    i spawn alot of Gameobjects with this Method:

    GameObject _g = Instantiate(collectable.GetGameObject());

    Before i spawn the Object i want to Check if this area is free for the full size of this object.

    If the object would collide i dosnt want to spawn this.

    How can i archive this?
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    2,013
  3. dara_mh

    dara_mh

    Joined:
    Mar 20, 2019
    Posts:
    3
    actaully i try it with this method

    Collider[] intersecting = Physics.OverlapBox(transform.position, collectable.GetGameObject().GetComponent<MeshRenderer>().bounds.size);

    But that dosnt give me the result what i want.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    871
    Can you tell us why that does not work for you? It seems to fit all your requirements, except that we do not know how large the bounds are when you get the game object from collectable.
     
  5. dara_mh

    dara_mh

    Joined:
    Mar 20, 2019
    Posts:
    3
    I think it is because i scale and rotate objects. This Physics.OverlapBox function check only the collision of the Rigidbody. The problem there is cant scale the rigidbody. Even i cant see where the rigidbody is, to figure out the overlap.
     
  6. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    871
    Couldn't you simple create a stand-in box that you know will always work (perhaps it's bigger than the worst-case Scenario if your prefab) and use that for the check, and then only spawn if there is enough space?
     
  7. dontdiedevelop

    dontdiedevelop

    Joined:
    Sep 18, 2018
    Posts:
    68
    Use the Physics.CheckSphere with layermask if returning false spawn your object