Spotlight follow enemy! (Help)

I have managed to use raycasting on selecting targeted enemies and making them red when targeted but now I need something like a spot light or a halo underneath the enemy to make it look better, so how would I got about having a spot light follow my targeted enemy? Thanks in advance :slight_smile:

using UnityEngine;
using System.Collections;


public class PlayerAttack : MonoBehaviour {
	public GameObject target;
	public float attackTimer;
	public float coolDown;
	public bool newon = true;

	// Use this for initialization
	void Start () {
			attackTimer = 0;
		coolDown = 2.0f;
	}
	
	// Update is called once per frame
	void Update () {

		if( Input.GetMouseButtonDown(0) && newon == true)
		{

			Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
			RaycastHit hit;
			
			if( Physics.Raycast( ray, out hit,  100 ) )
			{
				float distance = Vector3.Distance(hit.transform.position, transform.position);

				
				if(distance < 8) {
				string hitTag = hit.transform.tag;
				if (hitTag == "enemy")
				{
				Debug.Log( hit.transform.gameObject.name );
				target = hit.transform.gameObject;
					SelectTarget();
				}
					if( Input.GetMouseButtonDown(0) && distance > 8)
				{
						DeselectTarget();
					}
				
				}

			}
		}

		if (Input.GetMouseButtonDown (1)) {
				DeselectTarget();
				}

		if(attackTimer > 0)
			attackTimer -= Time.deltaTime;
			
		if(attackTimer < 0)
			attackTimer = 0;
			
	//attack press F key
		if (Input.GetKey (KeyCode.F)) {
		if(attackTimer == 0) {
			Attack();
			attackTimer = coolDown;
			}
		}
	}
	

	// CHANGE TO HIT NEAREST ENEMY?? IDK HOW MAN!!!
	private void DeselectTarget()
	{
		newon = true;
		target.renderer.material.color = Color.white;
		target = null;
	}

	private void SelectTarget()
	{
		newon = false;
		target.renderer.material.color = Color.red;

	}
	private void Attack() {
		AttackandHP eh = (AttackandHP)target.GetComponent ("AttackandHP"); 
						eh.AdjustCurrentHealth (-2);
				}

	
}

Create a spotlight pointed down, name it ‘EnemySpotter’, and attach this script:

using UnityEngine;
using System.Collections;

public class Spotter : MonoBehaviour {

	public float lightHeight = 4.0f;

	private Transform target = null;
	private bool doSpotting = false;

	void Update () {

		if (doSpotting)
			transform.position = target.position + Vector3.up * lightHeight;
	}

	public void StartSpotting(Transform t) {
		doSpotting = true;
		target = t;
		light.enabled = true;
	}

	public void StopSpotting() {
		doSpotting = false;
		light.enabled = false;
	}
}

Then modify your script above in the following ways as follows:

  1. At the top of the class add:

    private Spotter spotter;

  2. In start add:

    spotter = GameObject.Find(“EnemySpotter”).GetComponent();

  3. At line 81 add:

    spotter.StartSpotting(target.transform);

  4. At below line 74 insert:

    spotter.StopSpotting();

Note this might not be the way I would structure things. Typically I’m make an enemy responsible for changing its own color and spotlighting it self rather than have a third-party script do it.

P.S. You may need to change Update() to LateUpdate() in the Spotter script. Depends on the order your scripts execute.