- Home /
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?
Answer by cdrandin · Feb 02, 2013 at 07:59 PM
On the child node:
someNode.AddComponent(Rigidbody)
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:
var player:Transform;
function Update(){
// clone player's x/z, but not y
transform.position=Vector3(player.position.x,
transform.position.y,
player.position.z);
}
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() ins$$anonymous$$d 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.
Your answer
