Health problem

Im trying to make it that when i look at a lamp in my game, i lose health. When i do look at it my health drops from 100 to 99. Then it doesn’t recover nor go down again. Im pretty new to this kind of things. I’ve searched google for no answers.

Here is my code :

using UnityEngine;
using System.Collections;

public class LampLookAt : MonoBehaviour {

public Transform player;
public float HP = 100f;
public bool canSee = false;



void OnWillRenderObject() {

    RaycastHit hit;
	
	var dist = Vector3.Distance(player.position, transform.position);
	
	HP +=1;
	if(HP > 100) HP = 100;
	

    if (Physics.Raycast(player.position, (transform.position - player.position).normalized, out hit)) {

         if (hit.collider.gameObject == gameObject) {
			canSee = true;
			
		  Debug.Log("Your looking at the lamp");
				
			   if(dist > 15 && canSee == true){
				HP -= 1f;
			}
				else{
				
			    HP -=2f;
				}
				
				if(HP < 0){
				Debug.Log("Game Over");
			     HP = -0.08f;
				
			}else{
				canSee = false;
			}
			if(canSee == false){
				HP +=1;
		}
    }
	}

}

}

You are making it to complicated, I have a similar system but with fear, if the player is in the darkenss/ shadows it will decrease its fear level, if he stands in the light it will increase (i’m checking that with raycasts).

What you simply have to do is that your health is always increaseing overtime (but not by 1, more like 0.010) so that it slowly rises back, the higher the number the faster you recover, then you have to take into account that wenever you get damage that you have to outnumber the recovery speed so for instance:

    var Health:float=100;
    var RecoverRate:float=0.01;
    var DamageRate:float=0.05;
    
    private var LightDetected:boolean=false;
    
    function Update(){
    
    //this will increase helt overtime
    if(!LightDetected){
    Health =Health+ Time.time*RecoverRate;
    }
    
    //this prevents the healt to over go its limit.
    if(Healt>=100){
    Health =100;
    }
    
    //this prevents it to go negative
    if(Health<=0){
    Health =0;
    
    
    //and here you place your light detection code
    //I'l make an example with a boolean (not that a boolean //only won't do the trick.
    
    //do damage overtime when light detected
    if(LightDetected){
    Health=Health-Time.time*DamageRate
    }
    
    }
    
    }

Keep in mind that you can keep the boolean there as long as you make your detection sript change its value when looking at the light. Now this is a rough layout made on the fly so you probably need to tweak it, but ofcourse you won’t learn anything if I spoonfed it to you :). Good luck.

Something wrong with your logic.

  1. You always in crease health by one (line 13)
  2. You reduce health by one if he can see the lamp (line 25)
  3. You reduce health by 2 if he can’t see the lamp (line 29)
  4. You add 1 to health if he can’t see the lamp (line 40)

So when he sees the lamp, you have +1 and -1 = 0
When he can’t see the lamp, you have +1 then -2 then +1 = 0

So health doesn’t change…