Switching camera's opun PlayerDied

Actually, I’m surprised it works at all, if you have more than one enemy in the scene. If you’re looking to have a blur effect if any enemy is chasing, but no blur effect if no enemies are chasing, then you need to short circuit the test as soon as you find one.
For example:
Suppose you have three enemies, and the first enemy is chasing…

foreach (EnemyStateMachine enemyStateMachine in enemyStateMachines) 
        { 
            if(enemyStateMachine.enemyIsChasing == true) 
            {
                //_motionBlurComponent.weight.value = Mathf.PingPong(_timer * speed, 1);
                _motionBlurComponent.weight.value = 0.7f;
                _timer += Time.deltaTime;
            }
            if(enemyStateMachine.enemyIsChasing == false)
            {
                _motionBlurComponent.weight.value = 0f;
            }
        }

If you follow the flow of execution, the first enemy is detected as chasing, and motion blur is set, but then the second enemy is tested, and detected as not chasing and sets the motion blur off, same with the third…

foreach (EnemyStateMachine enemyStateMachine in enemyStateMachines) 
        { 
            if(enemyStateMachine.enemyIsChasing == true) 
            {
                //_motionBlurComponent.weight.value = Mathf.PingPong(_timer * speed, 1);
                _motionBlurComponent.weight.value = 0.7f;
                _timer += Time.deltaTime;
                return; //STOP CHECKING, you've already determined that one is chasing
            }
        }
        ///At this point, you've gone through all of the enemies and none are chasing
        ///So now it's time to set the weight to 0
        motionBlurComponent.weight.value = 0f;

I’m not sure what the timer or the speed and global enemyStateMachine is for.

I’m going to give you a different approach to this, which will save you using an Update every frame:

Start by adding a static event to EnemyChasingState

public static event Action OnChaseChanged;

And in both Enter() and Exit() add

OnChaseChanged?.Invoke();

This is my modified version of the BlurEffectOnTargeting.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HauntedPSX.RenderPipelines.PSX.Runtime;
using UnityEngine.Rendering;

public class BlurEffectOnTargeting : MonoBehaviour
{
    private EnemyStateMachine[] enemyStateMachines;
    public Volume volume;
    public float speed = 10f;
    private AccumulationMotionBlurVolume _motionBlurComponent;
    private float _timer;


    private void Awake()
    {
        volume = GetComponent<Volume>();
        volume.sharedProfile.TryGet(out _motionBlurComponent);
        _motionBlurComponent.weight.value = 0;
        enemyStateMachines = FindObjectsOfType<EnemyStateMachine>();
        EnemyChasingState.OnChaseChanged += UpdateBlur; //Static event

    }

    private void UpdateBlur()
    {
        foreach (EnemyStateMachine enemyStateMachine in enemyStateMachines) 
        { 
            if(enemyStateMachine.enemyIsChasing == true) 
            {
                _motionBlurComponent.weight.value = 0.7f;
                return;
            }
        }
        _motionBlurComponent.weight.value = 0.7f;
    }
}

Privacy & Terms