- Home /
Script acts differently since upgrade to Unity 3
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.
You're wrote a very detailed first question. That's quite rare on this site :D. Also you managed to highlight your code properly. Nice job with 14 years. It seems a lot of people around here don't have the time to phrase a good question. +1
Answer by Bunny83 · Apr 06, 2011 at 11:19 PM
Well, I'm not sure, but maybe it's because you attached your player to the trigger itself. Maybe they ignore collisions with childs by default now, can't say for sure, haven't tested it yet. You could try to parent your player to the platform instead of the trigger.
function OnTriggerEnter (other : Collider) {
// here i assign the player to the parent of the trigger
other.transform.parent = gameObject.transform.parent;
}
function OnTriggerExit (other : Collider) {
other.transform.parent = null;
}
Hope that helps.
Same problem. It works in the sense that the player is parented to the cube ins$$anonymous$$d of the trigger when entering, but the player still doesn't detach upon exiting.
Hmm, just tried such a setup and it works as expected. Well i don't use the newest version of Unity, we still use 3.0.0f5 so maybe it's changed in 3.3+. Unfortunately i can't test it right now because i don't have the newset version installed here.
$$anonymous$$aybe it's because of that change: "Parenting a collider to a rigidbody at runtime will now correctly add it to the rigidbody" --> http://unity3d.com/unity/whats-new/unity-3.2 look at Physics fixes
Yeah maybe that introduced a bug (or only partially fixed one). You should submit a bug report I think.
Well, i can't verify that behaviour at the moment like i wrote two comments above ;)
Answer by Justin Warner · Apr 07, 2011 at 12:24 AM
Jamie.
Like Bunny said, great question.
I did use the same exact code:
var Player : Transform; var Elevator : Transform;
function OnTriggerEnter (other : Collider) { Player.parent = Elevator; }
function OnTriggerExit (other : Collider) { transform.DetachChildren(); }
What I did, and it worked fine for me. Use a FP controller, normal. Add a box collider to an empty game object, and set it as a trigger (Make sure you tick that box). I put the script ON this trigger, and set the player, and set the other thing as the platform... The plane or whatever.
I then pushed play, I walked in to the collider, and I became a child of the platform. So it did work for me.
Here's the scene:
http://www.2shared.com/file/WLMmdZGW/TESTING_OTHERS.html
To open it, just so you know, drag and drop the folder (Main folder) to a place, goto Unity, File, Open Project, and select the whole folder...
Oh, you can ignore the other scripts... Their all broken or don't work right haha. Your code is saved as: TestOnTrigger.js. Haha. Sorry for any confusion XD.
Oh just a small hint: in windows you can create a context menu item to open a folder with unity. Here's my .reg file: Windows Registry Editor Version 5.00
[H$$anonymous$$EY_CLASSES_ROOT\Folder\shell\OpenUnity3\command] @="C:\\Program Files\\Unity\\Editor\\Unity.exe -projectPath \"%1\""
Still nor working. I walked into the trigger and the player became a child of the plain, but upon leaving the trigger the player remained a child. I also tried your other answer (the scripts with the tags and such), and they led to the same result. I'm pretty sure by now that it's a bug. I think I may still have the installer for Unity 2.6 somewhere on my computer, if this is a bug then I'm considering switching back to that temporarily until this is fixed.
Answer by Justin Warner · Apr 07, 2011 at 01:15 AM
Alright, I did a little bypass stuff to get it to work...
var Player : Transform; var Elevator : Transform;
function Update (){ Player = GameObject.FindWithTag("Player").transform; Elevator = GameObject.FindWithTag("Elevator").transform;
//if(Input.GetKeyDown("g")){
// Elevator.transform.DetachChildren();
//}
}
function OnTriggerEnter (other : Collider) { //print("D"); if(Player.parent != Elevator){ Player.parent = Elevator; } }
function OnTriggerExit (other : Collider) { //print("HEY"); Elevator.transform.DetachChildren(); }
You can take out the comments... But this DOES work... Ignore my last answer.
Um, however, to use, you must give the elevator/player tags... You CAN take the tag part out though like this:
var Player : Transform; var Elevator : Transform;
function Update (){ //Player = GameObject.FindWithTag("Player").transform; //Elevator = GameObject.FindWithTag("Elevator").transform;
//if(Input.GetKeyDown("g")){
// Elevator.transform.DetachChildren();
//}
}
function OnTriggerEnter (other : Collider) { //print("D"); if(Player.parent != Elevator){ Player.parent = Elevator; } }
function OnTriggerExit (other : Collider) { //print("HEY"); Elevator.transform.DetachChildren(); }
Hope this helps some =). The print's are for testing. KEEP the if statement in the OnTriggerEnter. For some reason when it's doing that, it doesn't let you do the OnTriggerExit... I think it might have something to do with like, it's a really deep child or something, I don't know that much of how Unity works...
But have fun! =)
Answer by Kruggerand · Apr 20, 2011 at 11:25 PM
I had a problem similar to this awhile back in a 2D platformer I was working on, I ended up just checking if gravity was activated (e.g. jumping or falling) and removing the parent then.
Answer by Tumasin · Apr 29, 2011 at 10:46 AM
We have some problem too with Unity 3.x We cannot port our game to Android because of the differences among the 1.7 and the 3.x physics
Your answer
Follow this Question
Related Questions
Public Class Error 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Unity 3.0 help mee 4 Answers
Hide and seek game in unity3d 0 Answers