Advertisement
evelynshilosky

Animal - Part 32

Feb 12th, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.88 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Animal : MonoBehaviour
  6. {
  7.     public string animalName;
  8.     public bool playerInRange;
  9.  
  10.     [SerializeField] int currentHealth;
  11.     [SerializeField] int maxHealth;
  12.  
  13.     [Header("Sounds")]
  14.     [SerializeField] AudioSource soundChannel;
  15.     [SerializeField] AudioClip rabbitHitAndDie, bearHitAndDie, foxHitAndDie, goatHitAndDie, sheepHitAndDie, wolfHitAndDie;
  16.     [SerializeField] AudioClip rabbitHitAndScream, bearHitAndScream, foxHitAndScream, goatHitAndScream, sheepHitAndScream, wolfHitAndScream;
  17.  
  18.     private Animator animator;
  19.     public bool isDead;
  20.  
  21.     [SerializeField] ParticleSystem bloodSplashParticles;
  22.     public GameObject bloodPuddle;
  23.  
  24.     enum AnimalType
  25.     {
  26.         Rabbit,
  27.         Bear,
  28.         Fox, Goat, Sheep, Wolf
  29.     }
  30.  
  31.     [SerializeField] AnimalType thisAnimalType;
  32.  
  33.  
  34.     private void Start()
  35.     {
  36.         currentHealth = maxHealth;
  37.  
  38.         animator = GetComponent<Animator>();
  39.     }
  40.  
  41.     public void TakeDamage(int damage)
  42.     {
  43.         if (isDead == false)
  44.         {
  45.             currentHealth -= damage;
  46.  
  47.             bloodSplashParticles.Play();
  48.  
  49.             if (currentHealth <= 0)
  50.             {
  51.                 PlayDyingSound();
  52.  
  53.                 animator.SetTrigger("DIE");
  54.                 GetComponent<AI_Movement>().enabled = false;
  55.  
  56.                 StartCoroutine(PuddleDelay());
  57.  
  58.                 isDead = true;
  59.             }
  60.             else
  61.             {
  62.                 PlayHitSound();
  63.  
  64.             }
  65.         }
  66.     }
  67.  
  68.     IEnumerator PuddleDelay()
  69.     {
  70.         yield return new WaitForSeconds(1f);
  71.  
  72.         bloodPuddle.SetActive(true);
  73.     }
  74.  
  75.         private void PlayDyingSound()
  76.     {
  77.         switch (thisAnimalType)
  78.         {
  79.             case AnimalType.Rabbit:
  80.                 soundChannel.PlayOneShot(rabbitHitAndDie); //Rabbit clip
  81.                 break;
  82.             case AnimalType.Bear:
  83.                 soundChannel.PlayOneShot(bearHitAndDie); //Bear clip
  84.                 break;
  85.             case AnimalType.Fox:
  86.                 soundChannel.PlayOneShot(foxHitAndDie); //Fox clip
  87.                 break;
  88.             case AnimalType.Goat:
  89.                 soundChannel.PlayOneShot(goatHitAndDie); //Goat clip
  90.                 break;
  91.             case AnimalType.Sheep:
  92.                 soundChannel.PlayOneShot(sheepHitAndDie); //Sheep clip
  93.                 break;
  94.             case AnimalType.Wolf:
  95.                 soundChannel.PlayOneShot(wolfHitAndDie); //Wolf clip
  96.                 break;
  97.             default:
  98.                 break;
  99.         }
  100.     }
  101.  
  102.  
  103.  
  104.     private void PlayHitSound()
  105.     {
  106.         switch (thisAnimalType)
  107.         {
  108.             case AnimalType.Rabbit:
  109.                 soundChannel.PlayOneShot(rabbitHitAndScream); //Rabbit clip
  110.                 break;
  111.             case AnimalType.Bear:
  112.                 soundChannel.PlayOneShot(bearHitAndScream); //Bear clip
  113.                 break;
  114.             case AnimalType.Fox:
  115.                 soundChannel.PlayOneShot(foxHitAndScream); //Fox clip
  116.                 break;
  117.             case AnimalType.Goat:
  118.                 soundChannel.PlayOneShot(goatHitAndScream); //Goat clip
  119.                 break;
  120.             case AnimalType.Sheep:
  121.                 soundChannel.PlayOneShot(sheepHitAndScream); //Sheep clip
  122.                 break;
  123.             case AnimalType.Wolf:
  124.                 soundChannel.PlayOneShot(wolfHitAndScream); //Wolf clip
  125.                 break;
  126.             default:
  127.                 break;
  128.         }
  129.     }
  130.  
  131.  
  132.  
  133.     private void OnTriggerEnter(Collider other)
  134.     {
  135.         if (other.CompareTag("Player"))
  136.         {
  137.             playerInRange = true;
  138.         }
  139.  
  140.     }
  141.  
  142.     private void OnTriggerExit(Collider other)
  143.     {
  144.         if (other.CompareTag("Player"))
  145.         {
  146.             playerInRange = false;
  147.         }
  148.  
  149.     }
  150.  
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement