Targeting script

Your in-game enemies won’t stand a chance against this smooth-moving targeting script.

This is a simple targeting script with smooth lerping transitions between game objects tagged “Target”. I wrote this for the Epic Space Game Template but it can easily be used in other projects.

  1. Add this script to your player or camera game object.
  2. Make a new tag named “Target”.
  3. Put some game objects around the scene and add the “Target” tag to them.
  4. You’ll also need to add a UI Text object to your scene and drag it into the public txtTarget slot.

The script features a smooth lerp camera rotation when cycling between targets and displays target object name and distance in units. Note I’m using C# lists and not arrays since I want to be able to add and delete freely during runtime.

Download “Targeting script” – Downloaded 393 times – 949 B

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class Targeting : MonoBehaviour {
    private Transform currentTarget;
    public List<GameObject> targets;
    private int currentTargetIndex = 0;
    public Text txtTarget;
    Quaternion targetRotation;
    private float t, tTime = 4;
    private float targetDistance;
    void Start()

        targets = new List<GameObject>();

    void Update()
        if (Input.anyKeyDown)                               //if any key is pressed, 
            t = tTime;                                      //break the lerping
            txtTarget.text = "";                            //erase the text
        //cycle target
        if (Input.GetKeyDown(KeyCode.Tab))
            if (currentTargetIndex < targets.Count - 1)
                currentTargetIndex += 1;
                currentTargetIndex = 0;

            targetDistance = Vector3.Distance(transform.position, targets[currentTargetIndex].transform.position);

            //output target name and distance
            txtTarget.text = targets[currentTargetIndex].name + " " + targetDistance;
            //set target rotation
            targetRotation = Quaternion.LookRotation(targets[currentTargetIndex].transform.position - transform.position);
            //reset the lerp
            t = 0;

        //if lerp has been reset, start lerping to next target
        if (t < tTime) //yes we are lerping { t += Time.deltaTime; //increase lerp timer t float perc = t / tTime; //calculate percentage of total lerp time duration transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, perc); //each update cycle moves closer to the target rotation } else if (t > tTime)                     //we've reached target rotation, time to stop lerping
            t = tTime;                          //stop lerping
        //determine the distance to target in each frame
        targetDistance = Vector3.Distance(transform.position, targets[currentTargetIndex].transform.position);
        //output target name and distance to the UI text box
        txtTarget.text = targets[currentTargetIndex].name + " " + Mathf.Floor(targetDistance);     
Scroll Up