- Home /
Making water impassible
Just as the title suggests... is there a way to make it so that a character cannot walk in to or on to water in my scene?
I just want to make the areas where I have water off-limits.
Answer by duck · Feb 25, 2010 at 04:30 PM
If you're using the physics engine for general movement and control of the objects in your scene, you might want to make some invisible walls around your water areas.
Create the wall meshes as normal in your 3D app, then just make sure they have a Mesh Collider attached, marked as static, and disable the renderer component - making them invisible.
Ugh, I was afraid of that answer, haha. I'm not much of a 3d guy, trying to do this exclusively with purchased assets and the build in editors :)
I guess I'll just have to flatten some cubes into walls and position them accordingly, but thats going to take FOREVER!
Depending on how your scene is set up, there may be a way to achieve this by doing some kind of 2d "scan" of your scene which detects which areas are water or not, and then dynamically building a collision mesh to fit. No small task though, but if you're more a coder than an 3d artists, this route might be preferable.
Answer by Kethis Celebes · Feb 27, 2010 at 05:44 AM
I'm going to play with your requirements a bit here, but this should be a LOT easier to implement. I'm a coder, so every time I hear "make a mesh in your 3d program" I look for a workaround...
Assuming that your water is all at a certain height, and the player never needs to go below that depth for any reason: Create a box collider the size of your bodies of water. Set it to Is Trigger. Lower it to the appropriate height.
Each frame your player is on the ground, check to see if he has a collision event with one of these water colliders. If he does not, remember his position. If he does, go back to that position and set his velocity to 0.
This approach should require little time to implement, as long as you dont frequently need to go below water level right next to the water.
Thanks! I thought about a solution similar to this, but I didn't think it would work because I didn't think to record my previous frame's position. Brilliantly simple!
And yes, my water level is the lowest reachable terrain on my map, so that should work perfectly :)
Answer by Ashkan_gc · Feb 27, 2010 at 06:44 AM
use triggers. create box colliders and put them around your water surface. or create a big sphere collider and put it inside your water and make it bigger than your water surface. then in OnTriggerEnter of those triggers don't allow the player to go inside triggers.
Your answer
Follow this Question
Related Questions
How to detect collision with shaders? 0 Answers
Island collider 3 Answers
Collision with specific object... 2 Answers
Collision detection without effects and without triggers 1 Answer
Collider Don't work 1 Answer