Hello, this is my first post on the unity forum.
I have made a small set up of my world, and implemented a basic sliding door, borrowing the mesh and animation from angrybots. My next goal would be to add keys/keycards to controll what doors a player can open at any given time. Since i plan on extending this to multiplayer later, I can’t just have a static list of keys the player have found, but rather would like to store them somewhere. The question being, where.
One idea I have been playing around with is having a script for this attached to each player, and when i trigger a message in the script it simply adds the key as a gameobject to the inventory.
Thats well and done and I could probably get that working. The question is then the otherway around.
When I enter the Trigger connected to the door, how do I validate the keys from the inventory. In any other language i would probably give myself access to a reference of the inventory, loop trough the keys in there, and look for a match for the key set in the door.
Does anyone have a small set of js to explain how I could accomplish this?
i would make it simpler. all dors have an unique id and your player has a list of all those ids. when he finds a key (which has the id of the related door) a variable for this id is set in this list at the id which tells that the player can open the door. when he attempts to open a door you check with its id if the player has found the key already.
i would not make keys inventory items as the player can drop them etc and then maybe is unable to finish the game.
Still takes us back to the question, that no mather how keys are handled, how do I actually read the key data when I enter the trigger?
Let’s say in my DoorScript.js I have the following
function OnTriggerEnter (other : Collider)
{
//Where other is the player
//Is it possible to do a GetComponents() on the Collider, or how do I access the
//key data in the fictional "KeyData.js" thats attached to the player?
}
and the documentation does not tell you wich elements you could use to get the info you need? you have the object which collides with the door and with this you can manipulate and query it like every other object you have a reference on. so get up a read on the documentation of collider. noone here is willing to write it down again for you.
also you can swap the direction. instead the door opens itself when the player enters its collider and has the key. just let the player have the collider and when he collides with the door query its id and open it when key is found. this way you could use the player collider for many other things like picking up powerups, show when something is in range/action is possible etc. you just need to determine which object you hit (tag, name, components).