Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Zuon94 · Feb 02, 2013 at 07:56 PM · parentwaterfollow playerfreezeposition

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zuon94 · Feb 02, 2013 at 08:43 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zuon94 · Feb 02, 2013 at 08:56 PM 0
Share

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

avatar image Wolfram · Feb 02, 2013 at 09:04 PM 0
Share

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)

avatar image Zuon94 · Feb 03, 2013 at 12:33 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

How to make a child object follow a target within the constrain of parent object 2 Answers

Camera smoothing of a Child Camera 1 Answer

How do I have multiple AI that follow the player but don't move into each other? 1 Answer

Having a friendly character follow player 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges