Hi.
I need a number of objects in each of my scenes to do something when the player is within a certain distance. Would it be best to attach a script to each object which checks the distance to the player, or attach a script to the player which loops and checks the distance to each object?
Target platform is android if it matters.
Thanks in advance!
using UnityEngine;
using System;
using System.Collections;
public class Example : MonoBehaviour
{
[SerializeField]
private float range = 10.0f;
private Transform t;
private Transform player;
private void Awake()
{
t = this.transform;
player = GameObject.FindGameObjectWithTag("Player").transform;
}
private void Update()
{
if (player)
print(player.name + " is " + Distance().ToString() + " units from " + t.name);
else
print("Player not found!");
}
private float Distance()
{
return Vector3.Distance(t.position, player.position);
}
}