In my game, I have a water plane, but when I go under the water level, it just looks like the water vanishes. I want an underwater effect similar to the ones found in many PS1 and N64 titles (Like Super Mario 64 - I don't want fog) so I made a colored, translucent cube, and made it so the top of the cube is touching the water level. I want to make it so when my player moves, the cube follows and rotates with the player on the X and Z axes, but does not move above or below the y position I set in the object's properties. How do I achieve this?
Answer by cdrandin
Feb 02, 2013 at 07:59 PM
On the child node:
someNode.rigidbody.constraints = RigidbodyConstraints.FreezePositionY
//So obviously we add a rigidbody to the child, if you wish to not use physics then do.
someNode.rigidbody.useGravity = false
someNode.rigidbody.isKinematic = true
That almost works, but what I'm looking for is to lock the global Y position, but keep the other axes local, and I can't quite figure out how to make the two work together.
Answer by Wolfram
Feb 02, 2013 at 08:49 PM
Place this script on your cube, assign "Player" in the inspector:
// clone player's x/z, but not y
EDIT: uh, you said your player is a parent of the cube? No, it won't work for this script. Move the Cube somewhere in the hierarchy where it isn't modified by other objects.
Thank you for this! This is exactly what I was looking for. For some reason I thought I had to have the cube as a child of the player. Guess I still have some learning to do. ^^;
Well, if you make it a child, you would somehow have to compensate any y movement of any parent, which can be difficult. In such a case it is easier to place it as a standalone object and just clone the/some coordinates from a source object.
Note technically it is possible that your cube lags behind your source by one frame, since the execution order of different Update()'s is unpredictable. If this is a problem for you (which it probably isn't, as I guess your cube is sufficiently large), you can fix it either by using LateUpdate() instead of Update() in this script, or you have to make sure your script executes after the player computes its new position (by modifying the execution order; select the script in the Project tab, then click on "Execution Order" in the Inspector and drag your script in there)
No need to do that now, as all seems to work smoothly, but I will keep your advice just in case a problem ever does arise. I scrapped the cube though, and replaced it with a cylinder that looks more natural.
And if anyone else wants to try this trick for getting decent water in Unity Indie, I suggest duplicating and flipping your water plane so you have underwater ripples on the surface. You could even go so far as to project caustics on the ground underneath, if you knew how.
But once again, thank you for helping me out with that bit of code. Right now, I'm merely an amateur.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
To help users post good questions and use the site effectively we have posted a user guide. Please check it out.
Answers and Comments
10 People are following this question.
Camera smoothing of a Child Camera
Make a simple tree
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. I am not using a Preafab.
When in the script lifecycle are parent-child relationships applied?
How to get local position & rotation from not parent object