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. Dismiss Notice

Within an area

Discussion in 'Scripting' started by Professor GIBS, Jul 3, 2014.

  1. Professor GIBS

    Professor GIBS

    Joined:
    Jun 30, 2014
    Posts:
    43
    I've been looking around through the fourms, and found this CLICK HERE. It's a way to enable & disable a light (and other things of course). I see how this could be very usefull... But let's say your at pointA, and there is a light switch at pointB. You don't want to be able to enable the lights all the way from pointA when the object is at pointB. I hope that makes since, but I was wondering how to make it where you can only access this switch if you're within a certain range of it. Lets say 2 meters. How could we do this?

    HERE'S THE CODE IN THE VIDEO (NOT MINE).
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnableComponents : MonoBehaviour
    5. {
    6.     private Light myLight;
    7.    
    8.    
    9.     void Start ()
    10.     {
    11.         myLight = GetComponent<Light>();
    12.     }
    13.    
    14.    
    15.     void Update ()
    16.     {
    17.         if(Input.GetKeyUp(KeyCode.Space))
    18.         {
    19.             myLight.enabled = !myLight.enabled;
    20.         }
    21.     }
    22. }
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    You can use a simple distance check:

    Code (csharp):
    1. float distance = Vector3.Distance(transform.position, player.transform.position);
    where player is a reference to "you" (i.e., the player object).

    But a more flexible approach would be to attach a trigger collider to the lightswitch object. Unity has a video tutorial on this very topic: Colliders as Triggers.
     
  3. Professor GIBS

    Professor GIBS

    Joined:
    Jun 30, 2014
    Posts:
    43
    Hey thank you so much for your help.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Happy to help!
     
  5. Professor GIBS

    Professor GIBS

    Joined:
    Jun 30, 2014
    Posts:
    43
    Okay, I looked into the Colliders as Triggers, and I liked the idea of hovering.. But, to be more specific... I have created a lightswitch. I want the character to have to Left Click the light switch to turn it on. But, they have to be within a reasonable distance from the switch of course. Anywhere I can learn to do that? Unless you want to just simply teach me yourself?
     
  6. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    Using a collider as a trigger is a perfectly valid way of doing it. Just increase the dimensions of the collider to the size you want. Then, when the character enters the trigger and has pressed the left click button, enable the light. Easy peasy.
     
  7. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    There I might go back to teh distance check, because you only need to do it when you click on a light. Then you use the Vector3.Distance and check if it's less than whatever is your required minimum for distance.
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    If it's just a one-off requirement, I agree with rrh; use a simple distance check.

    If you want to make something more general-purpose, use a trigger collider like zDemonhunter99 describes. For example, add a Sphere Collider to the lightswitch object. Tick Is Trigger. Set the radius to the maximum distance at which the player may activate the lightswitch.

    Then add a script like this:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class Lightswitch : MonoBehaviour {
    4.  
    5.     private bool inRange = false;
    6.  
    7.     void OnTriggerEnter(Collider other) {
    8.         if (other.CompareTag("Player")) inRange = true;
    9.     }
    10.  
    11.     void OnTriggerExit(Collider other) {
    12.         if (other.CompareTag("Player")) inRange = false;
    13.     }
    14.  
    15.     void OnMouseDown() {
    16.         if (inRange) FlipSwitch();
    17.     }
    18.  
    19.     void FlipSwitch() {
    20.         // Do whatever you need to flip the lightswitch.
    21.     }
    22. }
    (I just typed this straight into my reply, untested; there might be typos.)

    You could make this more general-purpose, such as:
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public abstract class RangedTrigger : MonoBehaviour {
    4.  
    5.     protected bool inRange = false;
    6.  
    7.     void OnTriggerEnter(Collider other) {
    8.         if (other.CompareTag("Player")) inRange = true;
    9.     }
    10.  
    11.     void OnTriggerExit(Collider other) {
    12.         if (other.CompareTag("Player")) inRange = false;
    13.     }
    14.  
    15.     void OnMouseDown() {
    16.         if (inRange) Activate();
    17.     }
    18.  
    19.     public abstract void Activate();
    20. }
    21.  
    22. public class Lightswitch : RangedTrigger {
    23.     public override void Activate() {
    24.         // Do whatever you need to flip the lightswitch.
    25.     }
    26. }
     
  9. Professor GIBS

    Professor GIBS

    Joined:
    Jun 30, 2014
    Posts:
    43
    Okay sweet thank you so much, for the part where I need to fill in the // Do whatever.... I am just going to hide the first model image and show the second do you think that would be a good idea?
     
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Yup. You could even make it more general like this:
    Code (csharp):
    1. public class ModelSwitch : RangedTrigger {
    2.     public GameObject object1;
    3.     public GameObject object2;
    4.  
    5.     public override void Activate() {
    6.         object1.SetActive(!object1.activeSelf);
    7.         object2.SetActive(!object2.activeSelf);
    8.     }
    9. }
    And then assign the models to object1 and object2.
     
  11. Professor GIBS

    Professor GIBS

    Joined:
    Jun 30, 2014
    Posts:
    43
    Okay sweet, been working lately so I will be messing around with all of this here soon! Thanks.