- Home /
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.
"pick up an object", could this be like a $$anonymous$$EY-object perhaps?
Answer by wccrawford · Oct 11, 2011 at 12:30 PM
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.
Answer by syclamoth · Oct 11, 2011 at 12:33 PM
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!)
Whoops, that's not very good. Let's do it in a more complex way!
And of course, you could modify that last version to actually keep track of specific keys, ins$$anonymous$$d of just counting how many you have.
Answer by elfinger · Oct 11, 2011 at 02:27 PM
Thanks for your help, I'm just getting started with level design, and all suggestions are appreciated.