• 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 smnerat · Nov 10, 2012 at 12:33 AM · transformparent

How to assign a parent based on the childs relative position.

Hey,

I have a moving object (Capsule) and a stationary object (Ring). I am trying to get the ring to be the parent object of the capsule if the capsule is within 5 units of the ring. (For the sake of discussion we will say along the y axis, the ultimate goal is to have the ring become the parent when the capsule comes within a circle around the ring of radius 5)

Anyways, I have successfully parented the capsule absolutely via script. When attempting to add the position part I don't get any errors, but the capsule does not become parented.

I have attached a copy of my script below. I'm hoping someone can see an error in my code or provide an alternative route to explore?

var Capsule : Transform; var Ring : Transform; var xtemp; var ytemp;

function Start () { }

function Parent (ytemp : int){

ytemp = Capsule.transform.position.y; if (ytemp >= 0 && ytemp <= 5) Capsule.transform.parent = Ring.transform; }

function Update() {

}

Comment
Add comment · Show 1
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 smnerat · Nov 10, 2012 at 01:32 AM 0
Share

I've tried a few things since I posted this without much success. I tried finding the difference between two Vector3's, one for the capsule and one for the ring, and going form there. There were a few other things I attempted, it seems like I am not setting the parent correctly, but after looking through the forums and Q/A's page I can't seem to find where I went wrong.

1 Reply

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

Answer by Seth-Bergman · Nov 10, 2012 at 01:54 AM

Nice attempt.. Here's a better way:

 var capsule : Transform; 
 var ring : Transform;
 var parentingDistance : float = 5.0;
 
 function Start(){
 capsule = GameObject.Find("NameOfCapsule").GetComponent(Transform);  // swap in name of your object
 ring = GameObject.Find("NameOfRing").GetComponent(Transform);        // here too
 }
 
 function Update(){
 if(Vector3.Distance(ring.position,capsule.position) < parentingDistance && !capsule.parent)
 capsule.parent = ring;
 else if(Vector3.Distance(ring.position,capsule.position) > parentingDistance && capsule.parent)
 capsule.parent = null;
 }

Vector3.Distance makes this a breeze! I believe this should work for you

Comment
Add comment · Show 4 · 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 smnerat · Nov 10, 2012 at 02:12 AM 0
Share

Executing this code gives me some errors, so I'm not sure if it works yet or not. On line 8 and 9 I get the error

"Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'."

avatar image Seth-Bergman · Nov 10, 2012 at 02:47 AM 0
Share

OOPS, my bad, fixing it now..

It should work now

avatar image smnerat · Nov 10, 2012 at 03:05 AM 1
Share

Almost there, I added ".position" after ring and capsule in the Vector3.Distance() arguements. Everything works great now. I sincerely appreciate the help!

avatar image Seth-Bergman · Nov 10, 2012 at 03:12 AM 0
Share

Good call! I missed that too.. fixed it now, glad I could help!

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

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

Accessing children of instances vs children of original prefab 1 Answer

getting object local direction? 1 Answer

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. WHY??? 6 Answers

Moving child transform also moves parent rigidbody 0 Answers

Make a simple tree 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