Click here to Skip to main content
15,887,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
got a few questions on coding in C#, i'll just ask one at a time so this doesn't get too lengthy.

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. which basically just raises the heart rate of the player when they get close to the enemy and lowers it when they get away from the enemy.

C#
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;
        }

    }
}


What I have tried:

C#
private int JumpRate;

public event System.EventHandler ChangeHeartRate;

protected virtual void OnHeartChanged()
{ 
     if (ChangeHeartRate!= null) ChangeHeartRate(this,EventArgs.Empty); 
}

public int heartT
{
    get
    {
         return JumpRate;
    }

    set
    {
         //#3
         JumpRate=value;
         OnHeartChanged();
    }
 }
Posted
Updated 25-Mar-18 6:40am

1 solution

First, I would probably change the heartT.set method to this. It will prevent you from accidentally handling heart rates that don't change:

C#
set 
{
    // Only change the field if the value has actually changed. If you 
    // want to only change the heart rate if the change is greater or 
    // less than a specific value, change the if statement accordingy
    if (value != JumpRate)
    {
        JumpRate = value;
        NotifyPropertyChanged(); // see next comment below.
    }
}


Second, I would derive my object from INotifyPropertyChanged so that I could respond to changes in the properties. That would preclude you from having to write custom event handlers. I don't think you need to be coding for WPF to take advantage of notifiable objects.
 
Share this answer
 
Comments
Member 13745684 25-Mar-18 14:11pm    
ok, i like the change in the first part of your answer, however, i do not know how to use " INotifyPropertyChanged " - how should i use it in the code, i see that you put in where it should be, but how do i get it to work properly?
#realJSOP 25-Mar-18 14:54pm    
Google is your friend.
#realJSOP 25-Mar-18 17:45pm    
If you can't wrangle google, your career as a developer is ruined. I google at least a dozen times every day, and I've been a programmer for almost 40 years. I wish to god that I'd had google as a resource when I was just starting out. But we didn't, and we had to nut through all of our questions, hoping to find the answer on our own, or in a printed book. You new guys have it much easier, by all stretches of the imagination.

When I googled "inotifypropertychanged", the very first page of results was FILLED with links that show how to do it.

(Using google is almost as critical a skill as learning how to use the debugger.)
Member 13745684 25-Mar-18 17:47pm    
...thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900