Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ReferenceWolf · Sep 17, 2021 at 05:20 PM · enemy aicombat

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image logicandchaos · Sep 17, 2021 at 09:26 PM 0
Share

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.

avatar image ReferenceWolf logicandchaos · Sep 17, 2021 at 09:30 PM 0
Share

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!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ReferenceWolf · Sep 20, 2021 at 06:58 PM 0
Share

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?

avatar image VisionElf ReferenceWolf · Sep 20, 2021 at 07:04 PM 1
Share

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.

avatar image ReferenceWolf VisionElf · Sep 24, 2021 at 07:40 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges