using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lighton : MonoBehaviour {
public GameObject light1;
public GameObject camera;
public double distance;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
distance = Vector3.Distance(light1.transform.position, camera.transform.position);
print(distance);//将灯和相机的距离打印出来
if (distance < 3)//距离判断条件
{
light1.SetActive(true);
}
else
{
light1.SetActive(false);
}
}
}
将上述脚本挂在空物体上,light1拖入指定的灯,camera拖入指定的相机。
此时运行时灯和相机的距离会实时打印在左下角,移动相机到适合的位置,记下现在的距离,然后可以在脚本中判断距离条件更改数值(脚本中“3”处)。