Search Unity

Underwater Script?

Discussion in 'Scripting' started by Spirrwell, Oct 4, 2013.

  1. Spirrwell

    Spirrwell

    Joined:
    Sep 17, 2013
    Posts:
    13
    Alright, I believe I've the basic concept down on how to do this, but I'm unsure of how to deal with water depth\collision. Basically I plan on making a water prefab that has a script that checks if the player is underwater, however, I can't simply test if the Player's Y position < the water Y position because it only accounts for position and not if they're actually inside that particular body of water.

    This requires a test of collision. So I have a plane of water, how do I test if the Player collides with it but also allow the Player to pass through it? Or I suppose "into" it. I suppose there needs to be some kind of collision box that extends downward that accounts for depth but isn't rendered. Kind of like the Source Engine.

    Any help is greatly appreciated.
     
  2. Marsupilami

    Marsupilami

    Joined:
    Sep 22, 2013
    Posts:
    21
    Check the "Is Trigger" flag of the prefab's collider then use the OnTriggerEnter and OnTriggerExit functions.
     
  3. Spirrwell

    Spirrwell

    Joined:
    Sep 17, 2013
    Posts:
    13
    Ah, that was extremely helpful, however, how do I make it so that the collision box is always beneath the water plane? I mean I can alter the center, but every single time I alter the scale, it's still extending from the center and not just up or down in the editor. I can set it up so in the code it automatically makes the collision box so it's underneath the water plane, but for the sake of simplicity is there any way to do this in the editor?

    Also, is there a way for me to auto-resize the collision box, so it's not going through walls or anything through the code? I mean in the editor it doesn't matter that it's going through walls a little bit, but in game this can have some odd effects on the player such as slipping through the level.

    Any help is much appreciated!
     
unityunity