I have what seems to me a pretty straightforward script that should work fine:
var target : Transform;
var cindyChar : Transform;
function OnTriggerEnter (other : Collider) {
if (other.CompareTag ("Player")) {
print ("onBoard");
cindyChar.transform.parent = target.transform;
}
}
function OnTriggerExit (other : Collider) {
if (other.CompareTag ("Player")) {
print ("off");
cindyChar.parent = null;
}
}
cindyChar is the player character (a character controller). The target is a block which moves up and down once per second (it moves 1 unit up and down, in discrete chunks). The script is assigned to a small trigger area parented to the block.
When the player steps onto the block, they begin moving up and down with the block, exactly as intended. But when they step off the block, they keep getting moved up and down, as if they were still parented to the block (often resulting in them falling through the floor). I’ve gotten it to work fine if I surround the block with OTHER trigger areas that all say “onTriggerEnter: cindyChar.parent = null”.
While standing in the area, the “onBoard” message seems to be continuously printed (rather than just once) which sort of confuses me. When you step off the area, the “off” message doesn’t display at all.
Hi!!..to me at least, OnTriggerEnter (or OntriggerStay) keeps on giving me (randomly, which is very weird) the same error and after fallinf the plaform, the character is still attached to the platform movement…
I forgot to mention, everything´s is triggered and tagged as “player”
I used this script without any problems:
function OnTriggerEnter (other : Collider) {
Debug.Log("Attach");
other.transform.parent = transform.parent;
}
function OnTriggerExit (other : Collider) {
Debug.Log("Detach");
other.transform.parent = null;
}
It just parents everything that enters the trigger to the elevator and if it leaves the trigger it gets unparented.
Do you use a CharacterController or a rigidbody for your char? You should also attach a kinematic rigidbody to the moving platform. Every object that is moved via scripting or animations that should interact with physic-objects need a rigidbody (in this case you need to set isKinematic to true)
What Unity version do you use? In some older version there was a bug in OnTriggerExit. In some cases it just didn’t fire. In my version (Unity 3.0) it works.
I use Unity 3.2. I tried your script and it has the same problem. The platform is a rigidbody.
I’m assuming this is an actual bug. I’m not sure if the way the “attach” debug keeps repeating infinitely is part of the same issue (I’m assuming it’s only supposed to happen once, until you exit the trigger and re-enter it?)
Hi!! I´m having exactly the same issue…even if I attach a rigidbody to the platform, after falling off, the player keeps on moving on the same direction…some kind of bug maybe? I´ve tried other versions ofr this script (even transform.DetachChildren() but the problem is still there.
Thanks
Edit:
So far, using OnColissionEnter, the problem is gone and after falling from the platform, the character doesn´t move no more at the same time of the platform. Here´s the code:
function OnCollisionEnter (other : Collision) {
if(other.gameObject.tag == "Player") {
other.transform.parent = transform;
}
}
function OnCollisionExit (other : Collision) {
if(other.gameObject.tag == “Player”) {
other.transform.parent = null;
}
}
Thanks
OK i can think of two things that may have the problem.
1: Is the player tagged as “Player”
if he is not it will go into the trigger but i will not pick up any collision for player. So please make sure that the player is tagged as Player in the inspector mode.
2: The Script
Now the main thing you have to look out for is attaching the script to the right object. So the script should be attached to a object which is a trigger not the main player. this is because we are looking to see if the player has entered the trigger. So if you have not make a New Game Object and make sure that it has a collider on it and tick the trigger check box. Once you have done that attach the trigger script to the new gameObject and run it. Also dont forget to attach your object transform and etc. Then walk into the trigger and the script should work.
I have tried your script and it works like a charm. that the possible things that i can think of that you might have missed out
I have same problem.
You will fix problem by adding rigidbody.
Use kinematic CHECK .