Before updating to Windows 10 you may need to return your Pro license. More information here

Make cube follow parent (player) on X & Z axis, but not Y?

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?

more ▼

asked Feb 02, 2013 at 07:56 PM

avatar image

5 11 13 15

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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
more ▼

answered Feb 02, 2013 at 07:59 PM

avatar image

1.7k 29 34 44

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.

Feb 02, 2013 at 08:43 PM Zuon94
(comments are locked)
10|3000 characters needed characters left

Place this script on your cube, assign "Player" in the inspector:

 var player:Transform;
 function Update(){
     // 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.

more ▼

answered Feb 02, 2013 at 08:49 PM

avatar image

11.6k 26 91 93

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. ^^;

Feb 02, 2013 at 08:56 PM Zuon94

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)

Feb 02, 2013 at 09:04 PM Wolfram

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.

Feb 03, 2013 at 12:33 AM Zuon94
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Feb 02, 2013 at 07:56 PM

Seen: 1725 times

Last Updated: Feb 03, 2013 at 12:33 AM