Picking a key to open a treasure chest

Hey there

I´m trying to let the user pick up a key that is hidden somewhere in the environment so that it can open a treasure chest that is also somewhere hidden.

Is it possible that the user finds the key, picks it up when passing over it (no need to press a keyboard key or mouse click) so that it can stay with him and then when he finds the treasure chest it opens if the user has the key?

The treasure chest i´ve downloaded from the asset store here

has a opening and closing animation that i can´t seem to figure out how to use with a key.

I´m still fairly new to Unity and programming so be patient. I´ve already done a blinking light, a spinning cube and i´m trying to code a book that opens so i guess i´m doing alright.

Many thanks in advance

What exactly is your question… “is it possible?” Yes, certainly.

Use a Boolean variable, hasKey which you initialise as false.

bool hasKey = false;

When the player passes over the key, set it to true. (look up triggers and collisions if you’re not sure how to tell when the player passes over the key)

hasKey = true;

When the player gets to the treasure chest, if the player has the key, open the chest:

if(hasKey) { animation.Play("ChestOpen"); }