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
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
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.