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.
How can this be done in the script of what is becoming the child.
– Owen_Burkchange "Other" to "gameObject". This will refer to itself as the object to change its parent.
– NimjaHey, I did this same thing, but my "objectYouWant" object's scale changed. I want my object to remain the same. My object is a sphere rigidbody that is controlled through user input.
– Eks-SquaredThis 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.
– DeadWabbit