c# code help -- events part 1

got a few questions on coding, i’ll just ask one at a time.

I would like, when the player walks in the field of view of the enemy, the enemy rushes at the player and then disappears, causing a raise in heart rate by 20 BPM.

I think events, PlayerInViewEvent, and RateIncreaseEvent are what would work for this. but i do not know how to get that all to work in the code, and i don’t know if those are even the best way to go about this. Can i please get some coding help.

here is the code i have so far.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeartRate : MonoBehaviour {

    public GameObject Player;
    public GameObject Enemy;
    float heartRateBase = 80; // 80 BPM
   public float heartRate = 100;

    float safeDistance = 5; // distance away form the enemy where heart rate goes down

    float relaxRate = 0.005f; //  how fast the player recovers
    float stressRate = 0.05f;// The closer the plauer is to the enemy the more it will increase

    void FixedUpdate()
    {
        // Find distance from enemy
        float dist = Vector3.Distance (Player.transform.position, Enemy.transform.position);
       
        // Check to see if player is safe
        if (dist > safeDistance)
        {
            // Decrease player heart Rate
            if (heartRate > heartRateBase)
                heartRate -= relaxRate;
        }
        else
        {
            // Increase the players heart rate depending on how close they are
            float rate = 1 - dist / safeDistance;
            heartRate += stressRate * rate;
        }

    }
}

You could use certain colliders as children of the enemy to represent it’s field of view, then when the player enters one of these colliders you could make the enemy do something.

For example: You could use a state-machine for the enemy using an enum; whenever the player is in sight, change the state of the enemy to aggresive, and do something based on that state.

As for making it disappear when it gets within range, You could enable or disable the meshrenderer of the enemy component based on the distance to the player.