Hi, so basically I just need help coming up with a way to make it so that the player cannot walk in water of strange shapes, such as seen in this picture:
As you can see, the water is not a regular shape, so placing a bunch of empty gameobjects with colliders wouldn’t be a very efficient solution. I tried exporting the terrain to blender and making a shape around the pond and giving that a collider, but that was extremely tedious, inaccurate, and would be an enourmous pain to implement for every source of water.
Does anyone have any better solutions? Any ideas would be appreciated, thanks.
To clarify: I dont want the player to be able to make contact with the water. I want them to be able to walk up to the water, but not go in it.
When I say the colliders wouldn’t be an efficient solution, I mostly mean it would be difficult to get the exact outline of the water, and it would take a very long time to accomplish. Not necessarily resource efficient, but it’s not time efficient or practal.
Try this: Take the water shape into Blender, grab the top face and extrude it up, create a new object from that and import it back into Unity. Place it above the water in scene and turn off it’s mesh renderer turn on it’s mesh collider
Calculating collisions with a bunch of simple BoxCollider isn’t very stressing CPU-wise. Just make a rough shape around the water and let the Physic handle the collisions.
It seems like you got caughtin the too-early optimization trap.
As others say, its not at all clear that a collider will be less efficient.
However there are a few ways you could cheat this.
Very often, water is done as a plane below the walked on terrain. If thats true in this case then the terrain that is walkable will always be at a higher Y coordinate.
SO you could cast a ray from the character down to see if the first collision is at water level.
Another thing you could do is gave the water plane or planes their own tag. Then you cast a ray down and see iof the first thing it hits has the water tag.
Finally, if the water is NOT under other areas but only where it shows, you could put it on a separate layer and cast a ray just to see if it hits that layer at all. That would be significantly more efficient then the other ray casts.