Search Unity

Activate/Deactivate Objects on Click or other Input

Discussion in 'Scripting' started by Radivarig, Apr 9, 2014.

  1. Radivarig

    Radivarig

    Joined:
    May 15, 2013
    Posts:
    121
    This I hope should be a good way of saving yourselves some fun debugging time when your code gets bigger, just write every behavior in a separate method (function) and trigger it depending on the ObjActivationStateScr state.

    Guide:

    1. Save these three scripts (be sure to name them the same)
    2. Create an Empty GameObject in the scene and call it "ActivationGO"
    3. Drag the ObjActivationScr on it
    4. Drag ObjActivationStateScr on your desired object (you can try it on a cube)
    5. Also on that same object drag SpawnPrefabOnClickScr (this is just a working example, you can add any and as much other scripts as you want like door opening etc.)
    6. Create a sphere with Rigidbody, make a prefab of it and drag to last scripts field "prefab".

    Everything should work. Look into inspector to change the distance from camera for the activation and show/hide the activation layout when the mouse hovers the object. In general, use ToggleActivation and set the value back if needed (like in third function).

    ObjActivationScr:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ObjActivationScr : MonoBehaviour {
    6.  
    7.     public float distance = 10f;
    8.     public KeyCode activationKey = KeyCode.E;
    9.     public bool showActivationSymbol = true;
    10.  
    11.     private bool isMouseHoveringTarget = false;
    12.     private RaycastHit hit = new RaycastHit ();
    13.     private GameObject target = null;
    14.     private bool isTargetValid = false;
    15.     private IsThisObjActivatedScr activationScr = null;
    16.  
    17.     void Update()
    18.     {
    19.         HandleTarget();
    20.         HandleActivation();
    21.     }
    22.  
    23.     void HandleActivation()
    24.     {
    25.         if(Input.GetKeyDown(activationKey))  ToggleActivation();
    26.         if(Input.GetKeyDown(KeyCode.Alpha1)) SetActivation(false);
    27.         if(Input.GetKeyDown(KeyCode.Alpha2)) SetActivation(true);
    28.     }
    29.  
    30.     void HandleTarget()
    31.     {
    32.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    33.  
    34.         if (Physics.Raycast(ray, out hit, distance))
    35.         {
    36.             isMouseHoveringTarget = true;
    37.             target = hit.collider.gameObject;
    38.             activationScr = target.GetComponent<IsThisObjActivatedScr>();
    39.         }
    40.         else
    41.         {
    42.             target = null;
    43.             activationScr = null;
    44.             isMouseHoveringTarget = false;
    45.         }
    46.  
    47.         isTargetValid = (activationScr != null);
    48.     }
    49.    
    50.     void ToggleActivation(){
    51.         if(isTargetValid)
    52.             activationScr.isActivated = !activationScr.isActivated;
    53.     }
    54.  
    55.     void SetActivation(bool value){
    56.         if(isTargetValid)
    57.             activationScr.isActivated = value;
    58.     }
    59.  
    60.     void ShowActivationSymbol()
    61.     {
    62.         if(isMouseHoveringTarget == false)
    63.             return;
    64.  
    65.         float offsetX = 20f, offsetY = 25f;
    66.         Vector3 displayPosition = Camera.main.WorldToScreenPoint(target.transform.position);
    67.         GUI.Label(new Rect(displayPosition.x - offsetX, Screen.height - displayPosition.y - offsetY, 200, 200), "[" + activationKey.ToString() +"]");
    68.     }
    69.  
    70.     void Debugging()
    71.     {
    72.         if(showActivationSymbol  isTargetValid)
    73.             ShowActivationSymbol();
    74.    
    75.         GUI.Label(new Rect(20, 20, 200, 200), "Target in range hovered: " + ( (target) ? "Yes" : "No.") );
    76.         GUI.Label(new Rect(20, 40, 200, 200), "Is target valid: " + ( (isTargetValid) ? "Yes" : "No.") );
    77.  
    78.         if(target != null)
    79.             GUI.Label(new Rect(20, 60, 400, 200), "Target trigger state: " + ( (isTargetValid) ? activationScr.isActivated.ToString() : "Not a valid target.") );
    80.         else
    81.             GUI.Label(new Rect(20, 60, 200, 200), "Target trigger state: No target.");
    82.     }
    83.  
    84.     void OnGUI(){
    85.         Debugging();
    86.     }
    87. }
    88.  
    ObjActivationStateScr

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ObjActivationStateScr: MonoBehaviour {
    5.     public bool isActivated = false;
    6. }
    7.  
    SpawnPrefabOnClickScr

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SpawnPrefabOnClickScr : MonoBehaviour {
    6.     public Transform prefab;
    7.     private bool isActivationScrAttached = false;
    8.     private IsThisObjActivatedScr activationScr = null;
    9.  
    10.     void Update ()
    11.     {
    12.         if(isActivationScrAttached == false)
    13.             FetchActivationScr();
    14.  
    15.         else {
    16.             if(activationScr.isActivated)
    17.                 BehaviourFunction();
    18.         }
    19.     }
    20.  
    21.     void BehaviourFunction()
    22.     {
    23.         Instantiate(prefab, this.transform.position, this.transform.rotation);
    24.         activationScr.isActivated = false;
    25.     }
    26.  
    27.     void FetchActivationScr()
    28.     {
    29.         activationScr = this.GetComponent<IsThisObjActivatedScr>();
    30.         isActivationScrAttached = (activationScr != null);
    31.     }
    32. }