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 matthew 2 · Feb 12, 2011 at 10:09 AM · javascriptparentchildpostion

setting a parent's variable to a child's variable without changing the position of the child according to a parent's position

hi. I am new to unity and i need help with setting a child's variable without setting the position of the child according to the parent or is there a method where you don't need a child or parent. I really need help with this and the child is a rigidbody with the script that has variables "x", "y" and "z" and the parent is a camera. The camera is supposed to follow the rigidbody (which is a sphere called "Sphere") without doing some kind of weird thing like making the rigidbody zoom at 1000 units per second with the camera following it and falling into eternity at the end of the flat world. In fact, it is supposed to move only when the arrow keys are pressed. Here is the part where the parent's variables are set and where the child's variables:

var x=0.0;
var y=0.0;
var z=0.0;
function Update(){
x=transform.Find("Sphere").position.x;
y=transform.Find("Sphere").position.y;
z=transform.Find("Sphere").position.z;
transform.position=Vector3(x,y+2,z-0.8);
};

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
1

Answer by StephanK · Feb 12, 2011 at 11:17 AM

If you want the camera to follow the sphere the camera should be the child of the sphere not the other way round. No scripting needed for that at all. (Unless you want to add some damping or smoothing)

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 Bunny83 · Feb 12, 2011 at 09:04 PM 0
Share

Well, that's the basic idea, but if it's a sphere and it's rolling on the ground it's better to have no parent-child relationship at all. Just use the smoothfollow script or similar scripts. A short look into the "unify community wiki" or the standard assets will help.

avatar image
0

Answer by DaveA · Feb 12, 2011 at 10:28 AM

Update() gets called every frame. So a couple problems here. 1. Find() is an expensive call. Do it once (in Startup() for example) and save that result, using it in Update, you'll get better performance. 2. 'position' is world-position. So you've essentially created a donkey/carrot relationship, where each frame Update is called to move the gameobject a few units from Sphere, which will (being a child) move with it. Zoom! Look into using localPosition instead, if you need relative placement. You shouldn't have to move them relative to each other (unless of course you have to), just move the parent, and the child follows.

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Make a simple tree 1 Answer

Problem with making child an object 2 Answers

Line up camera views 0 Answers

Find children by tag from Player 1 Answer

How can I get a parent GameObject of gameObject using Javascript? 6 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