Advertisement
evelynshilosky

PlayerState - Part 10

Jun 3rd, 2024
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerState : MonoBehaviour
  6. {    
  7.     public static PlayerState Instance { get; set; }
  8.  
  9.     // --- Player Health --- \\
  10.     public float currentHealth;
  11.     public float maxHealth;
  12.  
  13.  
  14.     // --- Player Calories --- \\
  15.     public float currentCalories;
  16.     public float maxCalories;
  17.  
  18.     float distanceTraveled = 0;
  19.     Vector3 lastPosition;
  20.  
  21.     public GameObject playerBody;
  22.  
  23.  
  24.     // --- Player Hydration --- \\
  25.     public float currentHydrationPercent;
  26.     public float maxHydrationPercent;
  27.  
  28.     public bool isHydrationActive;
  29.  
  30.  
  31.     private void Awake()
  32.     {
  33.         if (Instance != null && Instance != this)
  34.         {
  35.             Destroy(gameObject);
  36.         }
  37.         else
  38.         {
  39.             Instance = this;
  40.         }
  41.     }
  42.  
  43.     private void Start()
  44.     {
  45.         currentHealth = maxHealth;
  46.         currentCalories = maxCalories +10;
  47.         currentHydrationPercent = maxHydrationPercent +1;
  48.  
  49.         StartCoroutine(decreaseHydration());
  50.     }
  51.  
  52.     IEnumerator decreaseHydration()
  53.     {
  54.         while (true)
  55.         {
  56.             currentHydrationPercent -= 1;
  57.             yield return new WaitForSeconds(10);
  58.         }
  59.     }
  60.  
  61.  
  62.  
  63.     // Update is called once per frame
  64.     void Update()
  65.     {
  66.         // Calories Bar
  67.         distanceTraveled += Vector3.Distance(playerBody.transform.position, lastPosition);
  68.         lastPosition = playerBody.transform.position;
  69.  
  70.         if (distanceTraveled >=5) //I changed the amount from the video
  71.         {
  72.             distanceTraveled = 0;
  73.             currentCalories -= 10; //I changed the amount from the video (I also added 10 in the start method to keep the calories correct at the start)
  74.         }
  75.  
  76.  
  77.         // Testing the health bar
  78.         if (Input.GetKeyDown(KeyCode.N))
  79.         {
  80.             currentHealth -= 10;
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement