Search Unity

Enable a light based on position of another object

Discussion in 'Scripting' started by PatrickAllen, Jul 30, 2014.

  1. PatrickAllen

    PatrickAllen

    Joined:
    May 6, 2014
    Posts:
    16
    I'm trying to simulate an LED turning on by enabling a light when another object is close to a given position.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Activate14 : MonoBehaviour {
    5.     public GameObject player;
    6.     public ImageTargetBehaviour myTarget;
    7.     private NextButton2 playerScript;
    8.  
    9.  
    10.     void Start () {
    11.                 playerScript = player.GetComponent<NextButton2> ();
    12.         }
    13.     // Update is called once per frame
    14.     void LateUpdate () {
    15.         var pos = GameObject.Find("StopWire07").transform.position;
    16.         Debug.Log (pos);
    17.         if(myTarget.CurrentStatus == TrackableBehaviour.Status.TRACKED && playerScript.nextcounter >= 14) {
    18.             Debug.Log ("14 On");          
    19.             //renderer.enabled = true;
    20.             light.enabled = true;
    21.            
    22.     }
    23.         if(playerScript.nextcounter < 14) {
    24.             Debug.Log ("14 Off");
    25.             //renderer.enabled = false;
    26.             light.enabled = false;
    27. }
    28. }
    29. }
    The code above currently enables my light when my Image Target is being tracked and when my counter is 14 or greater. It also returns the position of the object "StopWire07" as -588.8, 6.4, -549.1

    What I want to do is add another condition to enabling the light. I want that condition to be that StopWire07 is in (or close to) the position indicated above. That start position will be a constant and will not need to update dynamically.

    This way I'm hoping that when StopWire07 moves out of position, the light will be disabled. And when it moves into position, it will be enabled.

    The problem I'm having is that I am very new to C# and I am unsure how to compare the current position with a constant, as well as how to included a plus/minus factor as I do not expect the object to be placed exactly back where it was.
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Why don't you use colliders to determine if an object is in range or not?
     
  3. PatrickAllen

    PatrickAllen

    Joined:
    May 6, 2014
    Posts:
    16
    Hello Fluzing,

    I'm very new to Unity. I didn't even realize that using colliders was an option until some further searches suggested it as a method. I'm not sure if they would be a "better" way or not. If I have time, I'll try comparing performance.

    I did manage to get my own script to work for my purposes.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Activate14 : MonoBehaviour {
    5.     public GameObject player;
    6.     public ImageTargetBehaviour myTarget;
    7.     private NextButton2 playerScript;
    8.  
    9.  
    10.     void Start () {
    11.                 playerScript = player.GetComponent<NextButton2> ();
    12.         }
    13.     // Update is called once per frame
    14.     void LateUpdate () {
    15.         var pos = GameObject.Find("StopWire07").transform.position;
    16.         Debug.Log (pos);
    17.         if(myTarget.CurrentStatus == TrackableBehaviour.Status.TRACKED && playerScript.nextcounter >= 14 && pos.x < -585f && pos.x > -595f && pos.z < -539f && pos.z > -559f)
    18.         {
    19.             Debug.Log ("14 On");          
    20.             //renderer.enabled = true;
    21.             light.enabled = true;          
    22.         }
    23.         else {
    24.             Debug.Log ("14 Off");
    25.             //renderer.enabled = false;
    26.             light.enabled = false;
    27. }
    28. }
    29. }
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Ok, but to be honest, it seems a bit complicated for turning on/off a light. Try to look into OnTriggerEnter() if you want to do something similar next time.
     
    PatrickAllen likes this.
  5. Medding3000

    Medding3000

    Joined:
    Dec 11, 2013
    Posts:
    45
    Why not use Vector3.Distance?
     
    PatrickAllen likes this.
  6. PatrickAllen

    PatrickAllen

    Joined:
    May 6, 2014
    Posts:
    16
    Thanks for your response Fluzing.

    I'm still very new to Unity and learning as I go.

    In this case, I'm not exactly sure how I would use OnTriggerEnter(). Would this more or less replace my If Statement?
     
  7. ScriptKid

    ScriptKid

    Joined:
    Aug 8, 2014
    Posts:
    23
    u have to create an empty gameobject with a colider and then turn set it to trigger in the inspector
    create a script with the ONTriggerEnter and OnTriggerExit function one for light on the other for light of

    when somthing with a colider enters the trigger colider the function is called and with exit the same
     
    PatrickAllen likes this.