Hello, I am looking for working script or tutorial to follow for one door with multiple keys and hud message after each one is picked up + how much u have and how much is total. Youtube tutorials are kinda outdated and I get tons of errors…
Might want to check out these tutorials so you’ll have some basic scripting knowledge: Unity Learn
Lol best tip ever like - if you want to have tons of money, get RICH!
Instead of learning two programming languages, im looking for a tutorial exactly for my case, without taking a course and certificate.
He is definetly right. You should know the fundamental of scripting and not just only copy past code.
But if you could explain what you want a bit more detailed I could help you.
Again, this is basic knowledge. I’m not going to spoonfeed any answers, but here’s some things to get you in the right direction:
You should store the amount of keys. Since you’re new, for now you can use PlayerPrefs. However, later on you’ll want to look into serialization to a binary file.
You could have multiple types of keys; you weren’t very descriptive so I’m not sure what you want exactly. Let’s say you have 3 different types of keys and you want to count how many total you have.
public int Type1Keys;
public int Type2Keys;
public int Type3Keys;
public int TotalKeysofAllTypes;
TotalKeysofAllTypes = Type1Keys + Type2Keys + Type3Keys;
Then you’d have to detect what door lock you’re using. You’ll probably know this since you’ll have to make a way to interact with the door. Let’s say you’re raycasting. Then you could have a tag on the door that states it is a door, then have a script on it to store its values.
An example of a door script:
using UnityEngine;
using System.Collections;
public class DoorScript : MonoBehaviour
{
public string KeyTypeAccepted = "Type3"; //example, 3 types of keys
public void AttemptUnlock(string TypeOfKey)
{
if (TypeOfKey == KeyTypeAccepted)
{
Debug.Log("Door unlocked with a " + TypeOfKey + " key!");
}
else
{
Debug.Log("You're trying to use key type " + TypeOfKey + " but the door needs " + KeyTypeAccepted);
}
}
}
Let’s say you want to click on a door to try to open it using raycasting, here’s how you’d get that:
using UnityEngine;
using System.Collections;
//this goes on your main CAMERA
public class ClickScript : MonoBehaviour
{
public string KeyTypeSelected; //this is what key you've selected
private Vector3 Forward;
private Ray Ray;
private RaycastHit RayHit;
void Start()
{
Forward = transform.TransformDirection(Vector3.forward);
}
void Update()
{
//if mouse button down
if (Input.GetMouseButtonDown(0))
{
//read on raycast here: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html?_ga=1.78322516.1319586730.1484072924
Ray = new Ray(transform.position, Forward);
//taken from raycasthit example here: https://docs.unity3d.com/ScriptReference/RaycastHit-distance.html
if (Physics.Raycast(Ray, out RayHit))
{
//check if object is tagged as a door
if (RayHit.transform.tag == "Door")
{
//now use the DoorScript to unlock
RayHit.transform.GetComponent<DoorScript>().AttemptUnlock(KeyTypeSelected);
}
else
{
Debug.Log("Object clicked isn't a door.");
}
}
}
}
}
Now go ahead, do some tutorials, and once you know what’s going on in those scripts, you’re ready ![]()
Thanks, but I don’t have key types, it is only one key. I mean there should be one door, and lets say 10 keys (same type, same model) spread in the map. Player find one - message 1/10. When player have all 10/10 keys, just touch the door to open. (I’ve made anim for door opening) And it should be touching instead of using key for opening, because it is android.
I’ve tried with raycasting with a script from one titorial, but I got like 20 red errors from script and I abandoned it…
Btw one script goes to door, one to maincamera (player camera) and which one goes to the key?
You can just store the keys on the click script (as I did with “KeyTypeSelected”). You can just change that to an int called Keys.
So then the logic is:
//check if object is tagged as a door AND you have more than 0 keys
if (RayHit.transform.tag == "Door" && Keys > 0)
{
//now use the DoorScript to unlock
RayHit.transform.GetComponent<DoorScript>().AttemptUnlock();
}
Obviously you can remove the “type of key accepted” etc… on the door script.