2

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.

Image describing the goal

  • What did you already try? – Ruzihm Mar 30 '21 at 18:20
  • @Ruzihm I have tried to add both the player and an enemy in a target group, but that doesn't align them the way I want them to align. I suspect that messing around with the weight and margin of the targets might give me what I want, but I'm not quite sure how. – Marius Franzén Mar 30 '21 at 18:32

1 Answers1

5

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.

Illustation

A quick proof of concept with the code I made. However the code is really just a proof of concept:

Quick mock

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;
    }
}
Pete
  • 569
  • 3
  • 9
  • I think this is pretty accurate. Some code would improve this answer greatly but this is a helpful partial answer, so I gave it an upvote. Any thoughts on how to position the green pivot point? – Ruzihm Mar 30 '21 at 19:34
  • 1
    @Ruzihm After making a simple implementation I think it looks quite close to what we see in Dark Souls. I ended up making the pivot point follow above the player, but making it lacking a bit behind. The makes a nice effect that looks like the camera needs to react when the player moves quickly. It did not try to move the pivot point, so that could maybe make it look better. With some more tinkering it could look great. – Pete Mar 30 '21 at 20:47
  • Excellent!! But look unless you get the player cube to start rolling around I don't think I can be convinced that this is an authentic dark souls camera... (just kidding of course ;] ) – Ruzihm Mar 30 '21 at 21:13
  • This is just what I was looking for! Thank you. Do you think there is a way to do this with cinemachine? Or can I simply implement this script alongside with cinemachine? – Marius Franzén Mar 31 '21 at 11:16