|
I am very new to programming. I am working on animating a door to open when the trigger is hit, and close when the trigger is exited. Here is my code. var door : GameObject; function OnTriggerEnter (collision : Collider) { if (collision.gameObject.tag == "Player") { door.animation.Play ("Bouncing Box"); } } function OnTriggerExit (collision : Collider) { if (collision.gameObject.tag == "Player") { door.animation["Bouncing Box"].speed = -1.0; } } Problem is it only works the first time. Is there a reset I have to put somewhere so it works everytime I hit the trigger? Thank you!
(comments are locked)
|
|
I think you have to put a boolean to say if the door is open ....do that if the door is not open...open it
I am not a programmer but after finishing a few tutorials I have this working code that works for me. So.. hope it works. With a few alterations it can work onCollider hit too. If you are a modeller that wants to have a system that works with every door tagged as "doorTrigger", I suggest whenever you import an animated door model, do the following
In the 3dPackage
A: Create two animations one opens, one closes.(I haven't tried to inverse the animation but it sound a good idea) For example frame 0 to frame 50 opens, 50 to 100 closes. In unity drag your model in your scene. A:add the script at the end of the answer in your game First Person Controller or whatever character controller you have. B: When you import the model add the animations in the root Game object. (an option in the import settings). This way you play all your animations for your model by finding your root game object. Use the same name for all your different doors or windows etc( this way whenever you hit a trigger ... called let's say doorOpen you always play an animation called "dooropen" even for different doors or a window etc... C: Give your trigger a tag like "doorTrigger" and check the isTrigger box in the mesh collider. D: (optional)Add sound for the door by adding a sound source to your first person controller or other controller and assign the audio clips under your attached script. Thank you so much. I will give it a shot. I really appreciate your detailed help!
Apr 05 '10 at 05:49 PM
Troy Halsey
Trying out your code, but I think there may be a few typos. Getting a lot of errors.
Apr 06 '10 at 06:54 PM
Troy Halsey
what kind of errors ?
Apr 07 '10 at 08:08 AM
alexnode
I think that the code works ... the second one. make sure you drag the script in your first person controller, add an audio source to the first person controller, in the script in the hierarchy panel under the first person controller define the audio clips. make sure that the name of the animation exists on your parent object.Make a trigger with a tag "doorTrigger" (ticked that as trigger in the collision properties). This trigger has to be below your root object that holds the animation. Store your animation in your root object and have your trigger as a child. what else?
Apr 07 '10 at 08:24 AM
alexnode
it is javascript ...
Apr 07 '10 at 08:25 AM
alexnode
(comments are locked)
|
|
To be honest, Animations aren't suited for such simple tasks as opening and closing doors. You may find it easier to just script the door, using Coroutines or something similar. Just "Transform" the door upwards and downwards when the player enters and exits the trigger. I can't provide more detailed help, because it sounds like it's a problem with the way you've animated the door, and I can't see your animation data. Sure I get that, but in this particular case, the door has an elaborate animation when opened and closed, similar to a bank vault spinning open. So I think I have to make the animation work somehow. Thank you.
Apr 05 '10 at 01:35 PM
Troy Halsey
Oh, I wasn't aware it was a complex door... I thought it was just a simple sliding door. Then yes, you'll need to work something out with your animations (however they're done).
Apr 05 '10 at 04:50 PM
qJake
(comments are locked)
|
|
Hi, I have a similar problem. I made an animation in 3dsmax and put the settings open door frame 30 to 0. When I try to activate the reverse mode(speed = -1)it will not start. Is there any way to make the model back to the starting position, as if the animation did not occur. I tried using rewind but did not work. I just want to close the door after opening animation
(comments are locked)
|

Alright, I realized I have the clip wrapmode set to Clamp Forever. If I put it to once, the door opens, but then seems to be frozen. I guess I really just need help writing this script properly. After reading for 3 hours, I haven't found a straightforward answer for a newbie like me. Thanks!