Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by JabberMonkey15 · Feb 07, 2019 at 04:49 PM · playerenemydamage

How can I get my enemy to hurt/kill player

Is there something wrong with this script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class EnemyAI : MonoBehaviour {
     private Animator anim;   
     private NavMeshAgent enemy; // invoking my NavMesh in the scene
     private GameObject player;
     private float AttackDistance = 3f; // distance at which my enemy will attack  the player
     private float WalkingDistance = 30f;// distance the enemy will start to run toward the player
     private int enemyHealth = 10;
     private bool isDead = false;
 
     private float timerCount = 4f;
     private float timerMax = 5f;
     public GameObject myEyes;
     
    
 
     // Use this for initialization
     void Start ()
     {
         player = GameObject.FindGameObjectWithTag("Player");// enemy will look for the Gameobject with the tag player
         enemy = GetComponent<NavMeshAgent>(); // im getting the component from start of game
         anim = GetComponent<Animator>(); // im getting the ANimator component
         
     }
     void damage(int damageAmount)
     {
         anim.SetTrigger("Got_Hit");
         enemy.speed = 0;
         enemyHealth -= damageAmount;
         timerCount = 0;
         if (enemyHealth <= 0)
         {
             anim.SetBool("Is_Dead", true);
             isDead = true;
             myEyes.SetActive(false);
         }
     }
 
     // Update is called once per frame its going to happen as i play 
     void Update ()
     {
         if (isDead)
         {
             timerCount += Time.deltaTime;
             if(timerCount >= timerMax)
             {
                 GameController.instance.enemiesOnScreen--;
                 Destroy(gameObject);
             }
             return;
         }
         timerCount += Time.deltaTime;
         if (timerCount <= timerMax)
         {
             return;
         }
 
         // enemy.destination = player.transform.position;  // assigning the NavMesh equal to the position of the Object labeled player
         enemy.SetDestination(player.transform.position); // this is the same as above 
         if (enemy.enabled) // if the NAvMeshAgent is Enabled (true or false)
         {
             float dist = Vector3.Distance(player.transform.position, this.transform.position);// getting the distance between the player and the zombie(this is Attached to)
             if(dist < AttackDistance) //if the enemy is less than the AttackDistance then the  Attack animation will play and the Run Animation will stop playing
             {
                 anim.SetBool("In_Range", true);
                 anim.SetBool("Player_Detect", false);
                 enemy.speed = 0f;
                 FacePlayer();
                 //ADD PLAYER DAMAGE SCRIPTY BITS
             }
             if (dist > AttackDistance && dist < WalkingDistance) // if the enemy is more than the AttackDIstance and less than the WalkingDistance the run animation will play and the Attack animation will stop playing
             {
                 enemy.SetDestination(player.transform.position);
                 anim.SetBool("Player_Detect", true);
                 anim.SetBool("In_Range", false);
                 enemy.speed = 1f;
                 FacePlayer();
             }
         }
         
     }
     void FacePlayer() //makes sure the enemy faces the player 
     {
         Vector3 direction = (player.transform.position - transform.position).normalized;
         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
 
     }
 
 }

Comment
Add comment · Show 1
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 tormentoarmagedoom · Feb 07, 2019 at 05:24 PM 1
Share

You really think saying "there is something wrong" we will read all the f...g code to find something wrong?....

UNITY ANSWERS FAQ

 What kind of questions can I ask here?
 
 Unity questions, of course! As long as your question is:
 
     detailed and specific
     written clearly and simply
     of interest to at least one other Unity user somewhere


0 Replies

· Add your reply
  • Sort: 

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

184 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 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

Enemy Isn't Hurting Player in Survival Shooter 0 Answers

My enemy doesn't die when my player attacks, when I try to click space bar, the player is hitting the enemy, but the enemy only acknowledges the damage after five or more hits, any advice? 1 Answer

[Solved] I can't call Method from Referenced Script 1 Answer

Turning player toward enemy after bool is true/button is pressed 1 Answer

Trigger is not detecting tag 2 Answers


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