Search Unity

Water Ripple Effect Programming Issue

Discussion in 'Scripting' started by robert-nally, Sep 1, 2019.

  1. robert-nally

    robert-nally

    Joined:
    Mar 5, 2014
    Posts:
    76
    Hey,

    So I made a water ripple effect and turned it into a prefab. When my player jumps into water that prefab gets instantiated onto my player. There is a trigger setup on the water to instantiate the water ripple prefab. Everything works well enough except I'd like the ripples to stay at the top of the water and currently they are at whatever height the player is at so when the player goes underwater the ripples do to.

    The water boxes or water height is always different so there is no fixed height I can use. Does anyone know of a good way I can keep the Y value of the water ripple prefab at the top of the water, but keep the X and Z value of the prefab following the player? How can I get the world space height of the top of a trigger box?
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    A more accurate option would be to use an empty gameobject parented to your player at the bottom of it and have a raycast pointing down from that empty, then if I'm thinking about how you've got this setup correctly you can instantiate the ripple on the hit.point just before the player actually hits the ground, maybe even do an if check for distance.

    Another thing to experiment with would be to have a trigger collider on this empty instead as that might be simpler but I think if you've got varying height constantly maybe a raycast would be best? The two methods could be worth trying either way.

    Regardless, empties I find are far more accurate than prefab models so I often use them to make sure everything in my projects work properly.
     
  3. robert-nally

    robert-nally

    Joined:
    Mar 5, 2014
    Posts:
    76
    The player can go into the water so it's not a hard collider it's just a trigger so I'm not sure how an empty game object with a collider would work with that. Can Unity have detection between two triggers?
     
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Ah you should have mentioned that detail it always helps people explain things, so what you would do is have the code attach to the empty itself and then have it search for your water collider, a tag would work fine for this. Put in some OnTriggerEnter code and have your effect instantiate and then it should all work. If you place it on the empty rather than the player it will all work from the origin point of the empty rather than your player model which means you can decide at what point you want the effect to happen by moving it up and down the axis you have dictating the height. So what I would do for accuracy is place the empty just at the player's feet and then you should have a much more accurate effect. Empties are absolutely great for this and one extra thing they're good for is if you want to have destruction or mess around with activating or deactivating gameobjects generally without messing up everything else you can make them the children of your empty and do all sorts without breaking everything.

    Hope that all makes sense, empties are fantastic for this sort of thing.