Lock-On Camera System in Unity (Target Locking for Combat Games)

Dynamic Camera Zoom System in Unity
Dynamic Camera Zoom System in Unity
July 14, 2026
Camera Shake System in Unity
Camera Shake System in Unity (Using Perlin Noise)
July 17, 2026
Dynamic Camera Zoom System in Unity
Dynamic Camera Zoom System in Unity
July 14, 2026
Camera Shake System in Unity
Camera Shake System in Unity (Using Perlin Noise)
July 17, 2026

Lock-On Camera System in Unity (Target Locking for Combat Games)

A lock-on camera system allows the player to focus the camera on a specific target, usually an enemy.
This system is commonly used in action games, RPGs, and combat-focused titles such as Dark Souls or Zelda.

When a target is locked, the camera automatically rotates to keep both the player and the target visible.


Why Lock-On Systems Are Important

Combat becomes much easier to control when the player can focus on a specific enemy.

  • Keeps the enemy in view
  • Improves combat readability
  • Makes movement circle around the enemy
  • Enhances the overall combat experience

Finding the Closest Target

The first step is detecting nearby enemies and selecting the closest one.

using UnityEngine;

public class LockOnSystem : MonoBehaviour
{
    public float lockRadius = 10f;
    public LayerMask enemyLayer;

    public Transform currentTarget;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            FindTarget();
        }
    }

    void FindTarget()
    {
        Collider[] enemies = Physics.OverlapSphere(transform.position, lockRadius, enemyLayer);

        float closestDistance = Mathf.Infinity;
        Transform closestEnemy = null;

        foreach (Collider enemy in enemies)
        {
            float distance = Vector3.Distance(transform.position, enemy.transform.position);

            if (distance < closestDistance)
            {
                closestDistance = distance;
                closestEnemy = enemy.transform;
            }
        }

        currentTarget = closestEnemy;
    }
}

Rotating the Camera Toward the Target

Once a target is selected, the camera should rotate to keep the enemy in view.

public Transform cameraPivot;
public float rotationSpeed = 5f;

void LateUpdate()
{
    if (currentTarget == null)
        return;

    Vector3 direction = currentTarget.position - cameraPivot.position;

    Quaternion targetRotation = Quaternion.LookRotation(direction);

    cameraPivot.rotation = Quaternion.Lerp(
        cameraPivot.rotation,
        targetRotation,
        Time.deltaTime * rotationSpeed
    );
}

Player Movement While Locked On

When lock-on is active, player movement usually becomes relative to the enemy.

Instead of moving forward in camera direction, the player circles around the target.

This allows strafing and better combat positioning.


Unlocking the Target

Players should always be able to cancel the lock-on.

if (Input.GetKeyDown(KeyCode.Tab))
{
    if (currentTarget != null)
        currentTarget = null;
    else
        FindTarget();
}

Switching Targets

Many games allow switching between nearby enemies while locked on.

This can be implemented by selecting the next enemy in the detection list based on camera direction.


Visual Target Indicators

Most games display a UI indicator over the locked enemy.

  • Target circle
  • Arrow indicator
  • Highlight outline

This makes it clear which enemy is currently selected.


Using Cinemachine for Lock-On Cameras

Cinemachine makes lock-on systems easier by allowing the camera to track multiple targets.

Using the Cinemachine Target Group, the camera can frame both the player and the enemy automatically.


Performance Tips

  • Use LayerMasks to detect only enemies
  • Avoid scanning the entire scene for targets
  • Limit lock radius to nearby enemies
  • Cache detected targets when possible

Conclusion

A lock-on camera system is essential for many combat-focused games. By allowing players to focus on enemies and automatically adjust the camera, gameplay becomes smoother and more intuitive.

When combined with smooth camera movement, collision handling, and zoom systems, lock-on targeting creates a polished third-person camera experience.


 

 

Leave a Reply

Your email address will not be published. Required fields are marked *


Skip to toolbar