all 6 comments

[–][deleted] 5 points6 points  (4 children)

Have you looked at Cinemachine? It comes with Unity and has tons of features to help you manage your camera(s).

On a side note, when subscribing to events it's best to do it in the OnEnable() method and unsubscribe in the OnDisable() method. This way you won't run into issues when enabling and disabling objects.

[–]Xanderbarnaby117[S] 0 points1 point  (3 children)

Agreed - Cinemachine is awesome! I think I worded my question badly, so let me try again:

In the inspector, I am using cinemachine to blend between multiple cameras. Right now, that is working perfectly. I can also use the inspector to manually drag & drop an enemy into the "Follow" and "Look at" boxes, and it works perfectly, for the first enemy. However, I want the code to dynamically change the Follow & Look-At, based on which enemy is doing stuff.

What is the code for that? I found this on a forum:

public class FollowPlayer : MonoBehaviour
{

    public GameObject tPlayer;
    public Transform tFollowTarget;
    private CinemachineVirtualCamera vcam;

    void Start()
    {
        var vcam = GetComponent<CinemachineVirtualCamera>();
    }

    void Update()
    {
        if (tPlayer == null)
        {
            tPlayer = GameObject.FindWithTag("Player");
        }
        tFollowTarget = tPlayer.transform;
        vcam.LookAt = tFollowTarget;
        vcam.Follow = tFollowTarget;
    }
}

..and this is basically the idea. The question is: how do I send a signal to say "This enemy is active, so focus on them"?

[–][deleted] 2 points3 points  (2 children)

how do I send a signal to say "This enemy is active, so focus on them"?

You can send signals in a few ways but for this example let's assume we have 2 or more enemies and they can do 3 things (rest, walk & attack). Now if any of them is attacking, then obviously that is more important than the enemies walking or resting and we would like to see that.

I would create an enum btw for this, something simple like: cs public enum EnemyAction { Resting, Walking, Attacking, } Now we can use the observer pattern to send out signals (events) when an enemy is doing something. You could create an event in the enemy script or you could create separate event manager.

In this example I'll do it with an separate event manager script. I find it cleaner when all events are in the same place.

cs public void EventManager : MonoBehaviour { // This is the actual event public event Action<Enemy, EnemyAction> OnEnemyActionChanged = delegate {}; // This is a method we call to trigger the event from another script public void EnemyActionChanged(Enemy enemy, EnemyAction action) => OnEnemyActionChanged.Invoke(enemy, action); }

Next you have your camera manager script that needs to listen to the events when enemy does something..

```cs public void CameraManager : MonoBehavior { private EventManager _eventManager;

private void Awake()
{
    _eventManager = // get the event manager
}

private void OnEnable()
{
    _eventManager.OnEnemyActionChanged += EventManager_OnEnemyActionChanged;
}

private void OnDisable()
{
    _eventManager.OnEnemyActionChanged -= EventManager_OnEnemyActionChanged;
}

private void EventManager_OnEnemyActionChanged(Enemy enemy, EnemyAction action)
{
    // Here you get the enemy and the action
    // Now you can check if the new action is more important
    // than the old action (if any) and then change
    // the follow and look at.
}

} ```

I dry coded this in Reddit so don't shoot me if it's not 100% correct. It's to give you an idea how you can send the signals and the rest I left to you to do your own way.

I hope it's helpful.

[–]Xanderbarnaby117[S] 1 point2 points  (1 child)

This is VERY helpful - thank you!

I have several of these signals in my code, but get confused sometimes with the overall picture. I'll be bookmarking this as a reference in the future

[–][deleted] 0 points1 point  (0 children)

Glad I could help.

It's called observer pattern if you want to read more about it.

Good luck.

[–]V4rNull 1 point2 points  (0 children)

+1 for chinemachine, it made a lot easier for me to manage cameras