- Home /
Key Pick up
Hi, just have a question if anybody can help me.
In a top-down game i am making, there is a simple puzzle around the idea that the player needs to pick up 3 items (a key) around a level and head to a door, which will open when the player has all 3 keys.
Does anybody know how to do this, where on a button press the key in the environment is destroyed and the game stores how many keys the player has picked up (1,2 or 3) and when the player has all 3 keys, the door will open, otherwise it will remain closed.
Thanks for any help.
Answer by BerggreenDK · Nov 16, 2011 at 07:52 PM
First of all, there are many solutions to this. Its the main idea of most "cave/adventure" ideas that you have to pick up something or enough to pass certain parts of a game.
So, you need some way to generic store your "requested items" if they are different. Like keys in different shapes, colors or materials.
A quick solution if you dont have too many items, could be to have a large array or string where each index is one of the specific items in your game. This array could get expanded as you build the game/level bigger and bigger.
When you pick up something, you just need to assign/attach each item a certain "index" value and this value then matches the array.
Like if you have a function called PickUpItem() which returns an index of 5, then the 5th item in your global itemsInLevel array is the one you have just picked up. Now you can go into the array or string if you like and assign a value to it. Like the count of how many you have or just the items "code/label" if you prefer to have that.
The door that should open, will only check if it can open once you pull the handle or get near it. This function could be called like 1/60 frames thats once pr. second and I believe thats often enough.
The doorscript could be made so it checked for certain indexs to be set or above zero and once this was true, then the door will open.
Does this give you a clue of how to proceed ?
Answer by vozochris_1 · Nov 16, 2011 at 09:43 PM
You can do it with bool's. At start make all bool's false. Keys must have a Collider, and if you want set it to Is Trigger.
In Key Script:
make 3 bool's
make Key Type's
bool Keyone = false;
bool Keytwo = false;
bool Keythree = false;
public KeyType keytype = KeyType.one;
make a enum
public enum KeyType(){
one,
owo,
three,
}
if Is Trigger
void OnTriggerEnter(){ }
if is not Trigger
void OnCollisionEnter(){ }
in void enter following
if (keytype == KeyType.one){
Keyone = true;
}
if (keytype == KeyType.two){
Keytwo = true;
}
if (keytype == KeyType.three){
Keythree = true;
}
Make an void Update() to check if Keys are picked up
void Update(){
if(Keyone){
if(Keytwo){
if(Keythree){
// Enter an animation or ... which makes door opened
}}} // Closes all if's
}
what kid of person are =, the way you represent code is good but funny. (Y) like it :)
Your answer
Follow this Question
Related Questions
Raycasting: Holding an Object 1 Answer
grab things?????with f 2 Answers
Buff System 1 Answer
How do I edit springjoint components in script? 1 Answer
Picking up objects and dropping them 0 Answers