Advertisement
johnnygoodguy2000

DamagePlayer.cs

Apr 28th, 2024 (edited)
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. public class DamagePlayer : MonoBehaviour
  7. {
  8.     public int damageToGive; // How much damage the object should do to the player
  9.  
  10.     // Start is called before the first frame update
  11.     void Start()
  12.     {
  13.        
  14.     }
  15.  
  16.     // Update is called once per frame
  17.     void Update()
  18.     {
  19.        
  20.     }
  21.  
  22.  
  23.  
  24.     void OnTriggerEnter2D(Collider2D other)     // When the object collides with something
  25.     {
  26.           Debug.Log("OnTriggerEnter2D called"); // Add this line to check if OnTriggerEnter2D is being called
  27.           Debug.Log("Colliding object tag: " + other.tag); // Log the tag of the colliding object
  28.         if (other.CompareTag("Player")) // Check if the colliding object is tagged as "Player"
  29.         {
  30.              HealthManager healthManager = other.GetComponent<HealthManager>(); // Get the HealthManager component from the colliding object
  31.  
  32.         if (healthManager != null) // Make sure the HealthManager component exists
  33.         {
  34.  
  35.             /*if (healthManager.currentPlayerHealth <= 0) // Check if the player's health is less than or equal to 0
  36.             {
  37.                 return; // If the player's health is less than or equal to 0, return
  38.             }*/
  39.            
  40.  
  41.             healthManager.TakeDamage(damageToGive); // Call the TakeDamage method of the HealthManager component
  42.  
  43.             other.GetComponent<AudioSource>().Play();// Play the damage sound
  44.             //knockback code
  45.             var player = other.GetComponent<PlayerController>();// Get the PlayerController component from the colliding object
  46.             player.knockbackCount = player.knockbackLength;// Set the knockbackCount to the knockbackLength
  47.             player.knockFromRight = player.transform.localScale.x < 0;// Set the knockFromRight to true if the player's scale is less than 0
  48.  
  49.             if (other.transform.position.x < player.transform.position.x)// Check if the colliding object's position is less than the player's position
  50.             {
  51.             player.knockFromRight = true;// Set the knockFromRight to true
  52.             }
  53.  
  54.             else
  55.             {
  56.                 player.knockFromRight = false;// Set the knockFromRight to false
  57.             }
  58.             //end of knockback code
  59.  
  60.  
  61.  
  62.  
  63.  
  64.            
  65.         }
  66.  
  67.            
  68.            
  69.         }
  70.     }
  71.    
  72.     //code not dealing damage to player
  73.     /* void OnTriggerEnter2D(Collider2D other)
  74.     {
  75.         if (other.name == "Player")
  76.         {
  77.             HealthManager.TakeDamage(damageToGive); // Call the TakeDamage method of the HealthManager component
  78.  
  79.  
  80.         }
  81.     }*/
  82.  
  83.  
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement