- Home /
C# coding help
I am trying to do a game where the player's heart rate is raised on screen when they enter a collider of a monster, and over time it decreases. I was thinking that just a text on screen with a UI element, that displays a float variable would work.
so basically, the heart rate would start at something like 64bpm and then when the player gets near a monster the heart rate would raise to something like 75bpm. then, when the heart rate gets to a certain point, maybe 120pbm, the player has a heart attack and its game over.
I am a little confused on how to get all this work. I know this is quite a loaded question. But It was better to do this all in one rather than segmenting it. I really need help with the coding with this whole project.
any help is welcomed.
Thank You
Answer by Cornelis-de-Jager · Mar 23, 2018 at 04:35 AM
Ok so ignore the collider part of it. use distance instead.
float heartRateBase = 100; // 100 BPM
public float heartRate = 100;
float safeDistance = 15; // Where heart rate goes down
float relaxRate = 1f; // sets how fast the player recovers
float stressRate =1f;// The closer it is to the monster the more it will increase
void FixedUpdate () {
// Find distance from monster
float dist = Vector3.Distance (player, monster);
// 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 he/she is
float rate = 1 - dist/safeDistance;
heartRate += stressRate * rate;
}
}
I don't have unity on this PC so lett me if its works.
Thank you this looks like its perfect, but I think im going to need a little assistance adding it to the scene. so 2 issues i have. I want to have more than one enemy/monster and i don't know how to do that, other than maybe finding a tag and tagging the monster as an enemy.
and the second issue is in the code itself, where there are compiler errors, which i think you did on purpose because i needed to add something to the field where you put "player, monster" - however i don't know what to put there.
i've attached a picture of a very very basic scene, where i just used basic shapes to get the coding to work. The sphere is the player, the cylinder is the enemy/monster. and the code on the right is to show what i did so far with it in visual studio.
any help is welcomed, thank you so much :)
Hi bunnysnake,
In the player and monster variables is where you put the position of the player and the monster. You will need other functions towrite that. However, I do recommend you go to the Unity learn page and try doing the tutorials on there. They are really good and show you how to code things step by step. $$anonymous$$y personal favorite for beginners is:
https://unity3d.com/learn/tutorials/s/space-shooter-tutorial
Its a beautiful game and easy to make.
i actually have already watched a lot of tutorials. coding is just not my fortay. so, im supposed to use the player and enemy locations, how would i do that? FindObjectType or would i find it with vectors? like local.transform.position ?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
AV Pro Video Playback Rewinding Issue -Android 0 Answers
Alternative to Update 1 Answer