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

Using colliders as a measure to instantiate gameobjects on top of each other?

Discussion in 'Scripting' started by Zymes, Dec 6, 2017.

  1. Zymes

    Zymes

    Joined:
    Feb 8, 2017
    Posts:
    118
    I want to place a game object (a ship) on water, but it should sink a bit. So I made a hidden plane under the water, which I raycast down to and instantiate the ship so it becomes a bit sunken down in the water.

    This works great.

    But... when I have two different ships that are not as big. The small will spawn under the water...

    How can I avoid this?

    My though was changing the box collider size downward of the small ship so it would be raised, but I noticed raycast does not go on box collider size, but the mesh itself, so the collider will overlap with the plane collider.

    Can I use the collider as a bounding box when instantiating on a raycast some how?

    Is there any other way of doing this?

    I don't want to have a variable on the small ships that is like "Y Spawn offset" or something like that if there is a better way.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    You could put an invisible keel on the small ship so that it touches the plane at a higher level. Just extend the collider you already have on it downwards. You might need to do some finagling to get the raycast and ignore raycast (not sure what you're raycasting for?) to work.

    At the end of the day though, you still kinda need to know what the intended waterline on the ship is, and go from there, either with a special gameobject marking it, or with always the same amount of collider below it, etc.
     
  3. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    Have two planes.. One for the same ships and one for the big ships.. have the small plane ignore collision with the big ships and vice versa
     
  4. Zymes

    Zymes

    Joined:
    Feb 8, 2017
    Posts:
    118
    But extending the collider does nothing. I need to extend the mesh downwards to make the keel bigger and it would touch the plane. Would have been really nice if I could just drag the collider downwards and it was all solved.

    I have my spawner set above the water tiles, and a hidden plane under the water and I raycast down so I know where to spawn the ships.

    But yeah... maybe two planes, a Y variable or modifying the mesh I guess.