I'm working on a small experimental project in Unity, and I want to lock on to an enemy. My camera behaves similar to Dark Souls camera, by using Cinemachine Free Look camera. Now I want to be able to click a button, and lock onto the enemy, similar to how the dark souls camera works, displayed in this video. I also made an image to explain what I'm looking for. I want both the player and the enemy be in the vertical center of the screen while locked on, but I want the camera to follow the player, while rotating around the enemy basically.
I have tried to add both the player and the enemy in a target group, but it doesn't behave the way I want it to. Sure, it looks at both the player and the enemy but it doesn't keep them aligned like I have tried to show in the image and the video.
To me in the video it looks like the camera is positioned around a pivot point (green) that is fixed to the player. The camera then points to the exact middle position (orange point) between the player and the selected enemy. It also looks like there is an max angle when the enemy jumps.
A quick proof of concept with the code I made. However the code is really just a proof of concept:
public class CameraScript : MonoBehaviour
{
public Transform enemy;
public Transform player;
public float cameraSlack;
public float cameraDistance;
private Vector3 pivotPoint;
void Start()
{
pivotPoint = transform.position;
}
void Update()
{
Vector3 current = pivotPoint;
Vector3 target = player.transform.position + Vector3.up;
pivotPoint = Vector3.MoveTowards(current, target, Vector3.Distance(current, target) * cameraSlack);
transform.position = pivotPoint;
transform.LookAt((enemy.position + player.position) / 2);
transform.position -= transform.forward * cameraDistance;
}
}