- Home /
How do I add Player Health and Enemy Attacking to my game
Hello there, I have recently been trying to make a game but I need a way for my player to take damage. My enemy can currently follow the player and track its location but when it reaches the player, it cant attack but i can attack the enemy. If anyone has any scripts for player health and enemy attacking that would be great. ill post my playercombat script and my enemy script for if theres a way you can find to implement this into my scripts i already have
Player Combat Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCombat : MonoBehaviour
{
//all variables
public Animator animator;
public Transform attackPoint;
public float attackRange = 0.5f;
public LayerMask enemyLayers;
public int attackDamage = 20;
public float attackRate = 1f;
float nextAttackTime = 0f;
// Update is called once per frame
void Update()
{
// Attack
if(Time.time >= nextAttackTime)
{
if (Input.GetButtonDown("Fire1"))
{
Attack();
nextAttackTime = Time.time + 1f / attackRate;
}
}
}
void Attack()
{
//Play Attack Animation
animator.SetTrigger("Attack");
//Detect Enemies In Range Of Attack
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
// Damage The Enemy
foreach(Collider2D enemy in hitEnemies)
{
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
}
}
void OnDrawGizmosSelected()
{
if(attackPoint == null)
return;
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
}
Enemy Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
//all variables
public int maxHealth = 100;
int currentHealth;
public Animator animator;
public GameObject AITracker;
public string AIDestinationSetter;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(int damage)
{
// Enemy Taking Damage
currentHealth -= damage;
// Play Hurt Animation
animator.SetTrigger("Hurt");
if(currentHealth <= 0)
{
Die();
}
}
void Die()
{
// Play Death Animation
animator.SetBool("IsDead", true);
// Disable The Enemy
GetComponent<Collider2D>().enabled = false;
(AITracker.GetComponent(AIDestinationSetter) as MonoBehaviour).enabled = false;
this.enabled = false;
}
}
any help would be greatly appreciated. Thank you in advance!
I would start by figuring out all the code you have posted, if you have a good understanding of C# what you are asking will be trivial.. You will never make a great game just patching other people's scripts together, you should understand almost every line of code in you game.
Hey there, I know what all of the code in my scripts do, I wrote it all out myself. I'm just not sure how I'd make the enemy damage me and add health to my character. I think I may be able to use the same code I used in my enemy script but I was wondering if there was a more efficient method. If you could help in any way it would be really appreciated, thanks!
Answer by VisionElf · Sep 20, 2021 at 02:21 PM
You already have done the part for the Player attacking the Enemy, it should be very similar when doing the Enemy attacking the Player.
Hello there @VisonElf, thank you for your reply. In my player attacking the enemy, I coded it to have to click the left mouse button (obviously), but how would i do this for the ai since it can't left click?
That's for you to decide. Basically you just have to think on "When do I want my enemies to attack me?" that could be distance, that could be a timer, that could be anything, I can't decide that for you.
Hey there again! Thank you for the reply. I'll take this into consideration when I'm trying to add enemy attacking to my player.
Your answer
Follow this Question
Related Questions
how to walk through enemy corpse after it is killed? 4 Answers
Enemy Combat AI 1 Answer
combat car tutorial? 1 Answer
Keeping gun on straight axis 2 Answers
How to differentiate between players in multiplayer game 1 Answer