Lock on Kingdom Hearts Style. (Just Camera Part)

Probably have a mind blank. How should I go about locking my cameras position behind the player (aka target) and using Transform.LookAt(selectedEnemy) to focus on the enemy. Lets just call the enemy “selectedEnemy” that I have already found in another script. Heres my camera. Anyway my goal is to lock onto the enemy and keep the player in view.

public class OrbitCam : MonoBehaviour{

    public Transform target;
    public float targetHeight = 1.7f;
    public float distance = 5.0f;
    public float maxDistance = 20;
    public float minDistance = .6f;
    public float xSpeed = 250.0f;
    public float ySpeed = 120.0f;
    private Transform selectedEnemy
    public int yMinLimit = -80;
    public int yMaxLimit = 80;
    public int zoomRate = 40;

    public float rotationDampening = 3.0f;
    public float zoomDampening = 5.0f;
    private float x = 0.0f;
    private float y = 0.0f;
    private float currentDistance;
    private float desiredDistance;
    private float correctedDistance;

   void Start (){
        Vector3 angles = transform.eulerAngles;
        x = angles.x;
        y = angles.y;
        currentDistance = distance;
        desiredDistance = distance;
        correctedDistance = distance;

        if (rigidbody)
            rigidbody.freezeRotation = true;

   void LateUpdate (){
        x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
        y = ClampAngle(y, yMinLimit, yMaxLimit);

        Quaternion rotation = Quaternion.Euler(y, x, 0);

        Vector3 position = target.position - (rotation * Vector3.forward * distance + new Vector3(0, -targetHeight, 0));

        RaycastHit collisionHit;
        Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y + targetHeight, target.position.z);

        position = target.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -targetHeight, 0));
        transform.rotation = rotation;
        transform.position = position;
    private static float ClampAngle(float angle, float min, float max)
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);

Thank you UA

Alright, so the big main chunks of code are SelectEnemy() and LockOnEnemy(), so have a look :wink:

Camera orbit


As said above, the “lock on” you see in Kingdom Hearts is just an over exageration of using GUI to represents focus. They simply find the nearest target and set focus to that object. Then switching between targets requires some mathy stuff. You need to calculate the difference between the focus target and the nearest target from the focus target, from both the left and right. SO when you press tab the focus goes to the right or alt+tab focus goes to the left. This requires taking the difference from your (focus - otherEnemy). I suggest to example plot the point so see the relationship between these points and see how can you tell which one is next time the focus target.

Once you have your focus target, have a targetGUI element ready. Use one of Unity’s function to calculate where this object is from the screen. Then place the targetGUI in the provided coordinates and there you go it focuses. Also, this SHOULD(?) have the targetGUI still follow the enemy movements.