- Home /
open a animated door
Hi i have some probleme with animation in unity :s I do a animation of a door in 3ds max. I export my door in fbx and i import this one in unity. Now i don't even understand how launch the animation of the door. I can see my animation on preview (inspector) but nothing on my "game window".
My purpose is when i approche the door with my first person controller and I press a key, the animation of the door start. How can i do that?
Thanks in advance
PS: sorry for my english =s
Answer by Wettechsupport · Jul 10, 2013 at 09:40 PM
Well, if you animate the door in 3DS Max, it won't animate physics (or it may, but it would run slowly)
I'm a beginner myself, but what I suggest... Make the model's origin on the door hinges, then rotate it in the script using transform.Rotate. You might want to disable to colliders while it's moving though.
hi, it's a animation of 2 sliding door (like a elevator) it's old door, so it's un little bit complicated animation. I find this http://www.youtube.com/watch?v=ZTTULr$$anonymous$$GVQI but can't import the animation like him, don't have the same inspector content :s I can't even import a animation :s
Answer by rowdyp · Jul 10, 2013 at 10:18 PM
well the first step is to probably add a collider on the door and make it a trigger by clicking the is trigger box. Then move the collider where you want it in correlation to the door. Next, add a script to the door that has the key press and and the plays the animation. Something like
function OnTriggerEnter (other : Collider)
{
if (Input.GetKeyDown ("space"))
animation.Play("opendoor");
}
forgive layout or possible errors in my code, I am on my phone.
Hi, just confused but wouldn't this make it impossible to duplicate the door with Ctrl D?
Answer by Oxidor · Jul 10, 2013 at 11:58 PM
OMG !!! that's work... but don't really know how it's work....i take a script found on the net, i put the rig of the door on legacy and it's work... This is the script:
var DoorGameObject : Transform; // This is a reference to the door GameObject that has your animation
private var HasTriggerBeenUsed : boolean = false; // This is to make sure that the button is not repeatedly pressed.
private var setTrigger : boolean = false;
function OnTriggerStay() {
if (Input.GetKeyDown("e") && !HasTriggerBeenUsed) {
DoorGameObject.animation.Play("Take 001");
setTrigger = true;
}
else if (Input.GetKeyDown("e") && HasTriggerBeenUsed) {
DoorGameObject.animation.Play("Take 001"); // Insert name of DoorClose animation instead of that
setTrigger = true;
}
if (setTrigger) { HasTriggerBeenUsed = !HasTriggerBeenUsed; }
}
Now i would like to play a sound when i launch the animation, how can i do this? thx in advance (and thx for your answer rowdyp )
use an animation event, first you have to duplicate the animation then drag the duplicated animation on the door in the scene. Then create a script to play animation with something like
var soundPlayer : AudioSource;
var opensound : AudioClip;
var Door : Gameobject
function playSound()
{
soundPlayer.audio.clip = opensound;
Door.audio.Play();
}
Drag the door object into door(duh) make sure your door has an audiosource attached to in and place it in the appropriate variable, do the same for the sound. Place this script on the door. Then go to Window>Animation in Unity, find the frame where you want to play the sound, right click then add Animation Event(), then select the appropriate function which was playSound() from the drop box. If you want a closing sound, just make a variable for the closing sound and create another function to play that sound, add another animation event for the closing door sound at the right frame.
Thank you for your answer I m a beginner in scripting and don't really understand your script. It's not just possible to start a sound when i press "E" ("E" launch my animation) only if i'm in the trigger?
not well yeah you could, but it may not sync up correctly depending on the audio clip, ect. Basically the first three lines create the variables in the inspector to which you can drag and drop the audio player (audiosource) attached to the door, the door itself, and the sound effect. The rest is just telling the audiosource what to play and when. If you want to just play the audio clip when you press the button, just open the script that plays the animation and add this
var opensound : AudioClip;
if (Input.Get$$anonymous$$eyDown ("e"))
{
audio.Play("opensound");
}
then just drag the sound effect into the opensound slot created in the inspector
Yes, you can start the sound without the trigger. The trigger thing is for make the door open only when in range.
i want activate the sound only when I'm in the trigger (don't want a door sound if i press E and i m not near my door) I just want add some line to my script to start a sound when my animation start and I'm in the trigger. But i don't know what i must add and where. But i will try to understand your first answer if you have no more proposition
Your answer
Follow this Question
Related Questions
Character controller problem? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
help in animation/code 1 Answer
Can you fix my code? (Audio for doors) 2 Answers