x


pickups used to open doors

I want my player to be able to pick up an object and use it to open a door (make a plane or box dissappear or be destroyed) when the player gets near it.

more ▼

asked Oct 11 '11 at 12:10 PM

elfinger gravatar image

elfinger
1 2 2 2

A question usually ends with a ? I don't see one.

Oct 11 '11 at 12:18 PM LegionIsTaken

"pick up an object", could this be like a KEY-object perhaps?

Oct 11 '11 at 01:15 PM BerggreenDK
(comments are locked)
10|3000 characters needed characters left

3 answers: sort newest

Thanks for your help, I'm just getting started with level design, and all suggestions are appreciated.

more ▼

answered Oct 11 '11 at 02:27 PM

elfinger gravatar image

elfinger
1 2 2 2

(comments are locked)
10|3000 characters needed characters left
public Transform assignThisInTheEditorItGetsDestroyed;
void OnTriggerEnter(Collider other)
{
    Destroy(assignThisInTeEditorItGetsDestroyed);
    Destroy(gameObject);
}

You put that script on a little trigger volume (collider with 'isTrigger' set to true), and then link it to a door object in the inspector. Then, if something enters the trigger, it'll delete the door, and then itself.

OR to do Zelda style one-use-antimatter-keys-

On your player (somewhere in your player script)

int keys = 0;

public void AddKey()
{
    keys++;
}

public bool CanOpenDoor()
{
    if(keys > 0)
    {
        keys--;
        return true;
    }
    return false;
}

Then, on your pickup a slight variant on the first one-

void OnTriggerEnter(Collider other)
{
    (name of your player script) player = other.GetComponent<Put the name of your player script here>();
    if(player)
    {
         player.AddKey();
         Destroy(gameObject);
    }
}

That gives you a key, and destroys the pickup.

Then on your door, something else similar (have a trigger volume along with an actual door- so that if the player enters the trigger with a key, the door opens)

public Transform thisIsTheDoorAssignItInInspector;
void OnTriggerEnter(Collider other)
{
    (name of your player script) player = other.GetComponent<Put the name of your player script here>();
    if(player)
    {
         if(player.CanOpenDoor())
         {
             Destroy(thisIsTheDoorAssignItInInspector);
             Destroy(this);
         }
    }
}

That's a little more sophisticated!

If you wanted to check for specific keys, you could go even further and do something like this-

[System.Serializable]
public enum KeyType
{
    Red,
    Blue,
    Yellow,
    Master
}

public int[] keys = new int[];
public void AddKey(KeyType type)
{
    keys[(int)type]++;
}

public bool CanOpenDoor(KeyType type)
{
    if(keys[(int)type] > 0)
    {
         keys[(int)type]--;
         return true;
    } else if (keys[(int)KeyType.Master] > 0)
    {
        keys[(int)KeyType.Master]--
        return true;
    }
    return false;
}

Then in your triggers for the pickups and the doors, use-

public KeyType key; // Assign this in the editor! 
//blah blah then in the trigger bit
player.AddKey(key);

// and then in the door;

if(player.HasKey(key))
{
    //Do things
}

(Thanks, @wccrawford, for the suggestion!)

more ▼

answered Oct 11 '11 at 12:33 PM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

Whoops, that's not very good. Let's do it in a more complex way!

Oct 11 '11 at 12:34 PM syclamoth

And of course, you could modify that last version to actually keep track of specific keys, instead of just counting how many you have.

Oct 11 '11 at 12:49 PM wccrawford

Oh yeah! I should post one of them as well.

Oct 11 '11 at 12:50 PM syclamoth

There you go! I need to go home now.

Oct 11 '11 at 12:58 PM syclamoth
(comments are locked)
10|3000 characters needed characters left

Then you'll need a few scripts.

When the player collides with the 'object', the script needs to set a variable (probably on the player) that says the player has the object.

When the player activates the door, the door's script needs to determine if the player has the object.

more ▼

answered Oct 11 '11 at 12:30 PM

wccrawford gravatar image

wccrawford
91 1 2

(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:

x280
x101
x9

asked: Oct 11 '11 at 12:10 PM

Seen: 752 times

Last Updated: Oct 11 '11 at 02:27 PM