Advertisement
LeeMace

Detect Collisions

Nov 29th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class DetectCollisions : MonoBehaviour
  6. {
  7.     private GameManager gameManager;
  8.     void Start()
  9.     {
  10.         //refernecing the game manager
  11.         gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
  12.     }
  13.  
  14.     void Update()
  15.     {
  16.  
  17.     }
  18.     //when the food and the animal collide they disappear and add score
  19.     //when the player and animal collide they lose lives
  20.     //when player destroys an animal they gain 5 points
  21.     private void OnTriggerEnter(Collider other)
  22.     {
  23.          if (other.CompareTag("Player"))
  24.          {
  25.              gameManager.AddLives(-1);
  26.              Destroy(gameObject);
  27.          }
  28.          
  29.         else if (other.CompareTag("Animal"))
  30.         {
  31.             other.GetComponent<AnimalHunger>().FeedAnimal(1);
  32.             Destroy(gameObject);
  33.         }
  34.     }
  35. }
Tags: collisions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement