Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bunnynsnake · Mar 24, 2018 at 03:50 PM · c#deathhealth

C# issue --- code assistance.

Good Moring,

I am trying to code a function in my game where the player gets jumped scared by an enemy and the heart rate is raised.

so far,i just have the player getting close to the enemy and it raises and when they get away from the enemy it decreases.

I need a jump in heart rate when the player gets scared. like for instance, in the picture i just have basic objects laid out in a hall. the white cylinders is the enemies and the blue sphere is the player. what im trying to code is, 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.

This ideally needs to be realistic in a sense, where the player starts at 80 bpm, and then they get scared and it jumps to 100 bpm - incremented by 20 bpm. then slowly decreases unless they get scared again.

The other thing I want to have some help with is a death function when the players heart rate gets to 300 bpm.

and finally, i would like to display the heart rate on the screen, using a UI element.

here is the code i have so far -- with comments

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class HeartRate : MonoBehaviour {
  
      public GameObject Player;
      public GameObject Enemy;
      float heartRateBase = 80; // 80 BPM
      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;
          }
  
      }
  }
  

Thank you for any help you guys can offer, I really appreciate it - i know this is a lot to ask for, but i thought it would be best this way, rather than in small sections. alt text

screenshot-50.png (394.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by RocketFriday · Mar 24, 2018 at 07:02 PM

Right well, first I just want to say; go look at Microsoft's C# stuff and Unity's C# stuff. Secondly, after doing that, you need to sit down, grab a pen and paper and think/plan your code out.


Now to answer this:

  1. "...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."

    • Use events; (Plan it out according to your unique needs)

      PlayerInViewEvent: Enemy will rush towards player and then disappears. RateIncreaseEvent: Increases heart rate by X amount, according to YourVariable.

      -

  2. "...the player starts at 80 bpm, and then they get scared and it jumps to 100 bpm - incremented by 20 bpm. then slowly decreases unless they get scared again."

    • Make a simple static bool for when events are in progress, if none are(yourBool = false), and your heart rate isn't 80 (or heart rate > 80) start a DecreaseHeartRateEvent or Method where you -= the heart rate by decrease speed variable.

    -

  3. "...death function when the players heart rate gets to 300 bpm."

    • if (bpm >= 300) { *call DeathEvent* }

    (Events are great! :D )


  4. "...display the heart rate on the screen, using a UI element."

Carve out several images in Ps or Gimp for various bpm (like every 10 or 20), fill the image with black and then make the heart rate squigglys white (or alpha), then in Unity set grayscale as transparency if you used white or alpha is transparency for that.

-

Then either write a simple shader, write a simple C# script or look into a different way to scroll a plain (horizontal) gradient image behind it and tint that (black&white) (also set grayscale as transparency) image to whatever color you want.

-

Lastly, use a UIMask Component to mask the gradient image with the foreground, heartrate image.

-

In this way, you can display a moving heart rate, as whatever color you want, without any background for that clean HUD look. (or with... whatever.)

For the shader script look at how shaders for water work. They scroll the normal map for the waves, use that same scroll with only one direction on the horizontal axis. Alternatively, simply write a C# script to move the background image in the same way, then repeat, and switch out the mask image when your bpm changes by a tenth or 2. Add in the sound and done.

I'm sure there are other ways, this is simply easiest solution I can think of (get creative!).

I hope you this answer helped you, I wish you the best of luck. I tried to explain it in the best way I can; you can message me from my profile if you still need help.


Please up vote this answer if you learned something, agree with it, or it worked for you so others can find a solution easier!

-

Thank you,
-RocketFriday
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bunnynsnake · Mar 25, 2018 at 03:43 AM 0
Share

thank you, i really like the way you sectioned the code out descriptions, however i am probably going to need a lot more help on each part. I will be messaging you if i can, or posting a more questions. Thank you again. and as for you saything that i need to write stuff down, what do you mean? i felt like i described all of what was needed pretty well...

avatar image RocketFriday bunnynsnake · Mar 27, 2018 at 12:28 PM 1
Share

You should take the time to watch some of the tutorials unity has provided, they will help you to understand each part. Or hire a programmer or join a $$anonymous$$m as a collaboration. You can't simply use Unity Answers to make a game for you, sooner or later that will bite you in the butt.

As for writing things down, that isn't for my sake but for yours. This is your game and if you are doing the program$$anonymous$$g, it's best to think out what you want, and how you'll code it in order to achieve that. $$anonymous$$uch like I did above but with more detail.

avatar image bunnynsnake RocketFriday · Mar 28, 2018 at 03:19 AM 0
Share

Thank you.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

465 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player death/ game over c# 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Alternative to Update 1 Answer

Can anyone find the problem with this code? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges