I have a script that keeps the player on a moving platform without sliding off. I made it when I had Unity 2.6. It worked perfectly. Here is the code.
var Player : Transform;
var Elevator : Transform;
function OnTriggerEnter (other : Collider) {
Player.parent = Elevator;
}
function OnTriggerExit (other : Collider) {
transform.DetachChildren();
}
Here is the setup. I have a basic cube for an elevator/moving platform as a test. I used the moving platform script from the 2D gameplay tutorial to make it move between two points vertically at a somewhat slow speed. A child of this cube is a GameObject with a box collider trigger component and the above script attached. The trigger hovers just above the cube, not touching anything at any time except the player. The Elevator variable is set to the trigger, the Player variable is, obviously, set to the player. Back when I was using Unity 2.6, everything worked well. If the player was inside the trigger and therefore riding the elevator cube, the player became a child of the trigger, and was able to ride the elevator without slipping off or falling through. When the player left the elevator and was not inside the trigger anymore, the player was detached. I made a sample scene with this script in it and built it, then didn't touch the scene after that. I made a new project, later updated to Unity 3, and then decided to try out the elevator script again. I made the exact same setup in the new project, but there's a problem - the player no longer detaches when exiting the collider. The player remains a child of the trigger, even after exiting it, which then causes the player to fall through the ground as the elevator moves downward. Thinking I had set something up wrong, I opened the old project and scene, which was in working condition last I had used it (I hadn't touched it since the upgrade). Same problem, the player wasn't detached when exiting the elevator. I played the build with the scene as compiled by Unity 2.6, and it worked, the player got off the elevator and was detached. So it seems that the upgrade to Unity 3 caused the script to not work right. I then tried this script, which I found on this answers site:
function OnTriggerEnter (other : Collider) {
other.transform.parent = gameObject.transform;
}
function OnTriggerExit (other : Collider) {
other.transform.parent = null;
}
Same problem. The player would become a child of the trigger upon entering, but wouldn't be detached when exiting. How do I solve this? I'm 14 and I don't know too much about scripting, I'm better with 3D modeling and such, so forgive me if I'm missing something obvious. Thanks!
EDIT: One thing I forgot to mention, I use the free version of Unity, not Pro. Not sure if that makes a difference or not, but I figured I'd say it anyways.
EDIT 2: Forgot one more thing, my character has a character controller attached. I have tried two scripts, the Lerpz tutorial control script, and the enhanced FPS controller from the wiki (both of which use character controllers). The problem occurs no matter which script I use.
EDIT 3: Sorry to keep editing this, but I'm using Unity 3.3.0f4.
EDIT 4: I've made a project/scene that shows the problem. If somebody with Unity 3.3.0 could download and test this to see if I'm not the only one with the problem, that would be great.
EDIT 5: Sorry to bump this again, but this hasn't been solved yet. I've asked on the Unity forums and only got one reply (which didn't work), and from the looks of a few other comments here I'm not the only one with physics-related issues in Unity 3. I submitted a bug report like somebody here suggested, included the sample scene and such, but got no response. Does anybody know what's going on? If I'm not able to get this worked out I'll have to go back to Unity 2.6.1... But as you can't downgrade project folders I would lose all my scenes I've made so far.