• 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
1
Question by benfattino · Jun 09, 2012 at 09:31 PM · parentmove

Move children object with parent

I have a object that moves with a script.

 ...
 transform.Translate (0.01* XVel * Time.deltaTime;, 0.01*YVel * Time.deltaTime, 0.01*ZVel * Time.deltaTime, Space.World);
 ...

To this mesh is attached a child object that moves with another script.

 var OBJ : GameObject;
 var OBJPos : Vector3;
 var OBJPosIN : Vector3;
 var maxX : float;
 var minX : float;
 var maxY : float;
 var minY : float;
 var moveSpeed : float = 1.0;
  var tChange: float = 0; // force new direction in the first Update
  var randomX: float;
  var randomY: float;
 
 function Start () {
 OBJPosIN = OBJ.transform.position;
 }
 function Update () {
 
 
 OBJPos =  OBJ.transform.position - OBJPosIN ;
     // change to random direction at random intervals
     if (Time.time >= tChange){
         randomX = Random.Range(-2.0,2.0); // with float parameters, a random float
         randomY = Random.Range(-2.0,2.0); //  between -2.0 and 2.0 is returned
         // set a random interval between 0.5 and 1.5
         
         tChange = Time.time + Random.Range(0.5,1.5);
     }
     transform.Translate(Vector3(randomX,randomY,0) * moveSpeed * Time.deltaTime, Space.Self);
     
     // if object reached any border, revert the appropriate direction
     if (transform.position.x >= maxX  || transform.position.x <= minX) {
        randomX = -randomX;
     }
     if (transform.position.y >= maxY || transform.position.y <= minY) {
        randomY = -randomY;
     }
     // make sure the position is inside the borders
     transform.position.x = Mathf.Clamp(transform.position.x, minX, maxX) ;
     transform.position.y = Mathf.Clamp(transform.position.y, minY, maxY) ;
     
     transform.position += OBJPos;
 
 
 }

I want the son to follow the parent object while still moving with his script. I try to insert position of parent object but the children object only move correctly in Y axis. Where I'm wrong?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Eric5h5 · Jun 09, 2012 at 09:44 PM

Use transform.localPosition for children.

Comment
Add comment · Show 2 · 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 benfattino · Jun 09, 2012 at 11:15 PM 0
Share

I replaced with transform.Position transform.localPosition. The child object is placed laterally respect to his parent (his position is -100 respect to the parent). When I play child object move right but its origin move to 0 (must be -100). How can I leave the origin of the same value at the beginning. (-100)? Help...

avatar image benfattino · Jun 10, 2012 at 01:15 AM 0
Share

Solving... change this value... Hope is stable...

maxX = Xmax - OBJ.transform.position.x + transform.position.x; $$anonymous$$X = X$$anonymous$$ - OBJ.transform.position.x + transform.position.x; maxY = Ymax - OBJ.transform.position.y + transform.position.y; $$anonymous$$Y = Y$$anonymous$$ - OBJ.transform.position.y + transform.position.y;

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make Explosion Child of Target 1 Answer

Make a simple tree 1 Answer

Move parent with child 1 Answer

How to move a gameObject with another gameObject but slower 1 Answer

How Can I move child object just in z axis cordinates? 2 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