- Home /
Trying to Open a Door
Ok, so I read through a bunch of the previous posted questions concerning this topic, but none of them really answered my question.. I'm extremely new to Unity and C#... What I am trying to do is open a door via an animation when the player approaches it. I have the doors already animated, and its under the door object when i expand it. How do I go about writing the right script, and setting up the colliders and what not that is needed? If anyone can help me you'd be a life saver.
Answer by AngryOldMan · Feb 28, 2011 at 02:10 AM
may i suggest using java script instead? it's alot easier especially if your new to scripting or you already know actionscript. it may look something like this (assuming you want the door to open and stay open)
var door : GameObject; private var HasBeenTriggered : int;
HasBeenTriggered = 0;
function OnTriggerEnter (collision : Collider) { if (collision.gameObject.tag == "teaPot") { if(HasBeenTriggered == 0) { door.animation.Play (); HasBeenTriggered = 1; } } }
Bob how would I go about setting up my different pieces to work with that script?
stick the script on an empty game object that has a collider with "is trigger" ticked. Change the bit that says "$$anonymous$$pot" to "Player" then tag your character Player in the inspector. $$anonymous$$ake sure your "door" has an animation on it with "play automatically" unchecked. Highlight the trigger object so it's properties appear in the inspector and drag the "door" gameobject into its script component,there will be a section that says "door Gameobject(none)" with all this set up you should just be able to move your character into the trigger area and it plays the door opening animation
Answer by Bunny83 · Feb 28, 2011 at 04:27 PM
Here's a C# script that opens a door. Once it's triggered the script deletes itself. In my case the animation to open the door is named "open". Make sure the WrapMode
of your animation is set to Once
. That way the door stays open. This script have to be attached to a GameObject that act as trigger. Add a collider, eg. BoxCollider to this GameObject, set it's isTrigger property and place it in front of the door. You have to drag&drop your animated door onto the animatedDoor variable of the script.
In C# (and also in JS) it's the scriptfiles name that have to be equal to the classname. In JS you can omit the class header and JS will automatically inherit from MonoBehaviour.
using UnityEngine; using System.Collections;
public class OpenDoor : MonoBehaviour { public Animation animatedDoor = null; void Start() { if (animatedDoor == null) Destroy(this); // The script needs the variable to be set } void OnTriggerEnter(Collider other) { if (other.tag == "Player") { animatedDoor.Play("open"); Destroy(this); // when triggered remove this script } } }
If you need more complex doors (eg. that closes automatically and so on) that involves a bit more logic in the script. Make yourself familiar with the Unity-api. Unity is very well documented, so take a look at the scripting reference and the runtime classes to understand the concept of Unity.
hey I'm sorry, but I typed up the code you gave me, and set up everything the way you said to. Only problem is that I don't have an animatedDoor vairable to drag and drop my door onto
Sorry, had a mistake. In C# inside a class everything is private by default. just add public infront of the fariable. I've changed my answer. Good you've spotted it ;) I didn't tried the sample, just wrote it from scratch.
Fantastic it worked.. You really helped me out big time I needed to get this work for one of my college game design classes. Which is also why I wanted to stick to C# because that is what they're $$anonymous$$ching us. I was wondering since you've been so helpful how would I go about making the camera shake, and have a dust particle effect fall from the ceiling?
Well, everything is possible ;) For the particle effect all you need is a particle emitter. To make the particles look like dust you may need to use a particle texture. It could be a lot of try and error to get the right look and feel but it shouldn't be a big problem. Camera shake effects have been asked already just search for it. If you can't find a solution that suits your needs ask another question. But first look here: http://answers.unity3d.com/questions/19559/is-it-possible-to-shake-the-camera
Your answer
Follow this Question
Related Questions
Open door as Character approaches 2 Answers
door opening 3 Answers
importing exporting Animation help. 1 Answer