x


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.

more ▼

asked Feb 28 '11 at 01:56 AM

cory gravatar image

cory
7 2 2 2

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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;
        }
    }
}
more ▼

answered Feb 28 '11 at 02:10 AM

AngryOldMan gravatar image

AngryOldMan
2.6k 12 21 47

Bob how would I go about setting up my different pieces to work with that script?

Mar 01 '11 at 12:43 AM cory

stick the script on an empty game object that has a collider with "is trigger" ticked. Change the bit that says "teapot" to "Player" then tag your character Player in the inspector. Make 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

Mar 01 '11 at 02:15 AM AngryOldMan
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 28 '11 at 04:27 PM

Bunny83 gravatar image

Bunny83
46.8k 12 50 210

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

Mar 02 '11 at 03:13 AM cory

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.

Mar 02 '11 at 09:54 AM Bunny83

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 teaching 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?

Mar 03 '11 at 02:31 AM cory

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

Mar 03 '11 at 02:41 AM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3929
x293
x200
x19

asked: Feb 28 '11 at 01:56 AM

Seen: 2455 times

Last Updated: Feb 28 '11 at 01:56 AM