What I’m trying to do is detect if the object with a certain tag is within range and if it is rotate the gun to face the targeted object. What I have now works on 1 object by name, but can’t figure out how to detect the object if it has another name. I have tried to use findobjectswithtag but am having no luck.
using UnityEngine;
using System.Collections;
public class LaserLook : MonoBehaviour {
public Transform target;
public float objectDistance;
public float rotationDamping;
void Start () {
target = GameObject.Find ("Random Material").GetComponent<Transform> ();
}
void Update () {
objectDistance = Vector3.Distance (target.position, transform.position);
if (objectDistance < 20f)
{
lookAtPlayer();
}
}
void lookAtPlayer()
{
Quaternion rotation = Quaternion.LookRotation (target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
}
}