- Home /
Script to parent player to object
I'm trying to build a fairly complex elevator script (10 elevators and 7 floors)
I'm trying to make the player parent to an elevator platform (elevator name is portOneEle) when the player triggers/collides with the collider called portOneEleArea (which is a box collider on top of the platform).
This is what I have, which, intuitively seems like I'm on the right track but... what the hell do i know?!
var Player:Transform;
function OnCollisionEnter(collision : Collision) {
Player.parent=portOneEle;
}
function OnCollisionExit(collision : Collision) {
Player.parent=null;
}
problem is... the script doesn't know what the object portOneEle is, and I'm not sure how to pass that into the script.
Answer by Doireth · Feb 02, 2013 at 10:37 AM
If you attach your script to the elevator you could
function OnCollisionEnter(collision : Collision)
{
collision.transform.parent = transform;
}
function OnCollisionExit(collision : Collision)
{
collision.parent = null;
}
Or to make sure you parent the correct object (Player in your case) you could instead:
function OnCollisionEnter(collision : Collision)
{
if(collision.gameObject.CompareTag("Player"))
collision.transform.parent = transform;
}
Though in this case you would have to make your Player object tagged as "Player". See Tag Manager
hmmmmm.... i guess that would work too... for some reason when i first had the idea i was thinking of triggers ins$$anonymous$$d of collisions... i'll go try it
Oh, then switch OnCollisionEnter() with OnTriggerEnter(). For example:
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.CompareTag("Player"))
{
other.transform.parent = transform;
}
}
so i tried both versions... and my elevator still does not "pick me up"
it has a sort of hit or miss tendency to either pass right through me or let me stand on it and ride it
it seems if i'm moving when it starts lifting it will pick me up... otherwise it just goes through me
is there a way to print the parent object and have it display for debugging?
to see if it's an issue with parenting or if it's an issue with something else?
Debug.Log (transform.parent.gameObject.name); should work.
Your answer
Follow this Question
Related Questions
How to make a box (or any object) appear on collide? 1 Answer
How do I stop an immediate collision with all objects from ocuring at the entry of game mode? 0 Answers
How can i modify the hirarchy of a game object 1 Answer
How to Change Parenting while in game? C# 1 Answer
Destruction Help!!!!! 1 Answer