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
You really think saying "there is something wrong" we will read all the f...g code to find something wrong?....
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