Enable a light based on position of another object

I’m trying to simulate an LED turning on by enabling a light when another object is close to a given position.

using UnityEngine;
using System.Collections;

public class Activate14 : MonoBehaviour {
    public GameObject player;
    public ImageTargetBehaviour myTarget;
    private NextButton2 playerScript;


    void Start () {
                playerScript = player.GetComponent<NextButton2> ();
        }
    // Update is called once per frame
    void LateUpdate () {
        var pos = GameObject.Find("StopWire07").transform.position;
        Debug.Log (pos);
        if(myTarget.CurrentStatus == TrackableBehaviour.Status.TRACKED && playerScript.nextcounter >= 14) {
            Debug.Log ("14 On");           
            //renderer.enabled = true;
            light.enabled = true;
           
    }
        if(playerScript.nextcounter < 14) {
            Debug.Log ("14 Off");
            //renderer.enabled = false;
            light.enabled = false;
}
}
}

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.

Why don’t you use colliders to determine if an object is in range or not?

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.

using UnityEngine;
using System.Collections;

public class Activate14 : MonoBehaviour {
    public GameObject player;
    public ImageTargetBehaviour myTarget;
    private NextButton2 playerScript;


    void Start () {
                playerScript = player.GetComponent<NextButton2> ();
        }
    // Update is called once per frame
    void LateUpdate () {
        var pos = GameObject.Find("StopWire07").transform.position;
        Debug.Log (pos);
        if(myTarget.CurrentStatus == TrackableBehaviour.Status.TRACKED && playerScript.nextcounter >= 14 && pos.x < -585f && pos.x > -595f && pos.z < -539f && pos.z > -559f)
        {
            Debug.Log ("14 On");           
            //renderer.enabled = true;
            light.enabled = true;           
        }
        else {
            Debug.Log ("14 Off");
            //renderer.enabled = false;
            light.enabled = false;
}
}
}

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.

1 Like

Why not use Vector3.Distance?

1 Like

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?

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

1 Like