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 Andreas Hagen · Feb 24, 2011 at 07:55 PM · rotationpositionparentchild

Behave like another object is parent?

Short and simple quastion: Gameobject A should behave like it is the child of gameobject B, even if it's not really the child. That means it should set its rotation and position like its gameobject B's child. Is it possible to do that with a script?

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
2
Best Answer

Answer by Peter G · Feb 24, 2011 at 08:50 PM

If you are using a physics controlled object, then you should use a fixed joint because it will do all the managing for you and you can set it up to detach, otherwise you could use a script.

Hinge Joints are your best choice, but I'll explain this script if you need it. It saves the distance between the child and the parent. Then it creates a new Vector3 that stores that position rotated by the parent's rotation. The child's position is the new offset + the parent's position.

How the loop works. Call ActivateParent() to activate or deactivate the loop. It sets isParented to flip, and if it flips from false to true, it calls the coroutine ParentObject described above. The coroutine then runs until isParented becomes false which is when you call ActivateParent() again. This way you don't have any unnecessary Update calls just checking to see if the boolean is true. It starts when you call ActivateParent() and ends when you call it again. Simple enough right.

var parent : Transform; private var offset : Vector3; private var isParented = false;

function ActivateParent () { offset = transform.position - parent.position; //offset is the difference between the 2 pos's isParented = !isParented; if(isParented) ParentObject(); }

function ParentObject () {

 while(isParented) {

      var curOffset = offset * parent.rotation;
      //this will rotate the offset position by the parent's rotation
      transform.position = parent.position + curOffset;
      //move this object        

      transform.rotation = parent.rotation;
      //This line is overly simplified.  You really need to account for the objects rotation 
      //at the beginning.

      yield;
 }

}

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
avatar image
1

Answer by Alec-Slayden · Feb 24, 2011 at 08:23 PM

you can either actually parent it to the object in a script like so:

GameobjectA.transform.parent = GameobjectB.transform;

and detach when you wish like so:

GameobjectA.transform.parent = null;

If you need B to have no children, you can create an empty game object at its position, set to follow it, and set that as the parent to A.

If you need A to have no parent (or a specifically different one), then the only way I can think of is to calculate the change to B's transform based on when A started to pretend to be a child. Here's a sample javascript for adjusting position. For rotation some fiddling will need to be done with Rotate Around I believe.

var fakeParent : Transform;

private var parentOriginP : Vector3; private var isPretending = false; private var childOriginP : Vector3;

function Update(){

 if (isPretending){
     var positionChange = fakeParent.position - parentOriginP;
     transform.position = childOriginP + positionChange;
 }    

}

function StartPretending(){ //call this when you want to pseudoparent. childOriginP = transform.position; parentOriginP = fakeParent.position; isPretending = true; }

Overall I believe it'll be much easier to manage if you can get away with transform.parent on the fly.

EDIT: fixed code problem, previously would have compounded the 'child' position to keep moving

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

Camera follow ball along cylinder 1 Answer

Where to find original unity Parent/Child script? 2 Answers

How can I identify a non-uniform scaled mesh, and fix it? 1 Answer

How can I make a First Person Character Controller a child of a moving platform without it affecting the rotation of the Character Controller. 1 Answer


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