- Home /
Parenting Object With Triggers?
How can I make an object become a child of another object upon entering a trigger and detach upon exiting?
EDIT : I figured out how to do this and I am going to leave my working script for others to use ;)
It should "hook" the corner object and turn around the corner. I have created a script for this but I'm not too sure where to attach it or whether to identify the trigger or the rotating (Parent) object.
Here's my script: [FIXED]
var rotator : Transform;
var player : Transform;
function OnTriggerEnter(collider : Collider){
if(collider.tag == "Trig"){
print("Parented");
player.transform.parent = rotator.transform;
}
}
function OnTriggerExit(collider : Collider){
if(collider.tag == "Trig"){
print("Un-Parented");
player.transform.parent = null;
}
}
Just out of curiosity, put a Debug.Log() statement inside the OnTriggerEnter() and OnTriggerExit() and see what happens. What may be happening is that the rotation of the parent takes the object back out of the collider.
Thanks for the info. I have only just managed to pull this off with two scripts. I put a script that disables the rotation parent when the function begins, upon entering the trigger enables the rotation parent and disables the forward acceleration script on the player, upon exiting the trigger disables the rotation parent and re enables the forward acceleration script. I just had to put these on the player rather than the trigger. The only thing I have issues with now is that when the player exits the trigger, it leaves on an angle of 94 degrees when it should be leaving at 90. I kinda feel awkward now that I have asked this question and figured it out myself :/
$$anonymous$$y only problem is that the script I have for rotating the rotation parent is not disabling upon starting the function.
Answer by bloodyatheist · May 03, 2014 at 10:34 AM
just a little fix to your code you don't really need to save the player separately cause you get it in the trigger function anyway:
var rotator : Transform;
function OnTriggerEnter(collider : Collider){
if(collider.tag == "Trig"){
print("Parented");
collider.transform.parent = rotator.transform;
}
}
function OnTriggerExit(collider : Collider){
if(collider.tag == "Trig"){
print("Un-Parented");
collider.transform.parent = null;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613144131im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I can't move gameobjects or create parents/children in the hierarchy menu, what's going wrong? 1 Answer
Destroy(other.gameObject) isn't destroying parent, only children 2 Answers
Procedural/Random Generation of Solar systems 2 Answers
Changing the parent's position based on a child's variable 0 Answers
check a object is parent or not? 1 Answer