Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

My key system script is having some issues

Discussion in 'Editor & General Support' started by gamingcat, Jun 11, 2018.

  1. gamingcat

    gamingcat

    Joined:
    May 10, 2014
    Posts:
    2
    I recently got back into using Unity and I've made a very basic key system in Unity, basically you click on the key gameobject, it turns a bool into true, destroys the key gameobject, then allows you to open a door. However my issue is, that rather than having to click on the key/door gameobjects, you can click anywhere and it automatically destroys the key and door gameobject. Ive been looking for quite a long time and havent been able to find any solutions for this issue, here is my code:

    keys.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class keys : MonoBehaviour {
    6.    
    7.      void Start()
    8.      {
    9.          Update();
    10.      }
    11.  
    12.     void Update()
    13.     {
    14.        
    15.         if(Input.GetMouseButtonDown(0))
    16.         {
    17.             GlobalVars.haveKey = true;
    18.             Destroy(this.gameObject);
    19.         }
    20.        
    21.     }
    22.    
    23. }
    door.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class door : MonoBehaviour {
    6.    
    7.      void Start()
    8.      {
    9.         Update();
    10.      }
    11.    
    12.     void Update()
    13.     {
    14.        
    15.         if(Input.GetMouseButtonDown(0) && GlobalVars.haveKey == false)
    16.         {
    17.             Debug.Log("no key");
    18.         }
    19.        
    20.         if(Input.GetMouseButtonDown(0) && GlobalVars.haveKey == true)
    21.         {
    22.             Destroy(this.gameObject);
    23.         }
    24.     }
    25.  
    26. }
    GlobalVars.cs (so both scripts can use the haveKey boolean)
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public static class GlobalVars{
    6.       public static bool haveKey = false;
    7. }
     
  2. ldhongen1990

    ldhongen1990

    Joined:
    Dec 18, 2015
    Posts:
    61
    Your current code detect only mouse click, so ANY mouse click will be taken as though you clicked on the key.
    This is your problem.

    To correct this, the condition checks will have to change.
    If your key and door is a 3D object, a simple way would be to perform a raycast test from screen space (where you click) to the world space (where the key is in the 3D world).

    If the test fail, it means the user did not click on the key.
    Otherwise, the conditional check is successful and you may now destroy the key and flag your haveKey bool.

    Perform the same raycast test on the door object.

    Here are the things you need to read up on:
    https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html
    https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
     
  3. gamingcat

    gamingcat

    Joined:
    May 10, 2014
    Posts:
    2
    Hmm, alright, I'll do some looking into Raycasting, thanks for the resources mate! :D