- Home /
Making an object a child on collision
Is there a script that will make an object a child of another when they are touching each other. only in contact, so when they are not touching each other my object is not a child of the other one
note i am new to scripting
Answer by xToxicInferno · Aug 20, 2010 at 03:45 AM
This should work nicely:
function OnTriggerEnter (other : Collider) { if(other.gameObject.tag == "objectYouWant") { other.transform.parent = transform; } }
function OnTriggerExit (other : Collider) { if(other.gameObject.tag == "objectYouWant") { other.transform.parent = null; } }
This is for when they are trigger's, if they aren't the use:
function OnCollisionEnter (other : Collision) { if(other.gameObject.tag == "objectYouWant") { other.transform.parent = transform; } }
function OnCollisionExit (other : Collision) { if(other.gameObject.tag == "objectYouWant") { other.transform.parent = null; } }
Simply attach one of these scripts to the object you want to be the parent, then tag the other object accordingly. Just make sure you change the 'objectYouWant' tag.
How can this be done in the script of what is beco$$anonymous$$g the child.
change "Other" to "gameObject". This will refer to itself as the object to change its parent.
Hey, I did this same thing, but my "objectYouWant" object's scale changed. I want my object to remain the same. $$anonymous$$y object is a sphere rigidbody that is controlled through user input.
This did not help. The script was half created. When putting it into test. Came with many errors and im stuck trying to fix them. Thanks for the half answer.
This code has to be out of date or something.. Nothing works right, and i cant fix his errors.
Your answer
Follow this Question
Related Questions
Unparent object on collision? 3 Answers
Parenting tutorial 1 Answer
How do I increase speed after collision? 2 Answers
Collider2D.GetContacts() always returns only 1 contact 1 Answer
Tree collision 1 Answer