Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- public class DamagePlayer : MonoBehaviour
- {
- public int damageToGive; // How much damage the object should do to the player
- // Start is called before the first frame update
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- }
- void OnTriggerEnter2D(Collider2D other) // When the object collides with something
- {
- Debug.Log("OnTriggerEnter2D called"); // Add this line to check if OnTriggerEnter2D is being called
- Debug.Log("Colliding object tag: " + other.tag); // Log the tag of the colliding object
- if (other.CompareTag("Player")) // Check if the colliding object is tagged as "Player"
- {
- HealthManager healthManager = other.GetComponent<HealthManager>(); // Get the HealthManager component from the colliding object
- if (healthManager != null) // Make sure the HealthManager component exists
- {
- /*if (healthManager.currentPlayerHealth <= 0) // Check if the player's health is less than or equal to 0
- {
- return; // If the player's health is less than or equal to 0, return
- }*/
- healthManager.TakeDamage(damageToGive); // Call the TakeDamage method of the HealthManager component
- other.GetComponent<AudioSource>().Play();// Play the damage sound
- //knockback code
- var player = other.GetComponent<PlayerController>();// Get the PlayerController component from the colliding object
- player.knockbackCount = player.knockbackLength;// Set the knockbackCount to the knockbackLength
- player.knockFromRight = player.transform.localScale.x < 0;// Set the knockFromRight to true if the player's scale is less than 0
- if (other.transform.position.x < player.transform.position.x)// Check if the colliding object's position is less than the player's position
- {
- player.knockFromRight = true;// Set the knockFromRight to true
- }
- else
- {
- player.knockFromRight = false;// Set the knockFromRight to false
- }
- //end of knockback code
- }
- }
- }
- //code not dealing damage to player
- /* void OnTriggerEnter2D(Collider2D other)
- {
- if (other.name == "Player")
- {
- HealthManager.TakeDamage(damageToGive); // Call the TakeDamage method of the HealthManager component
- }
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement