So i’m really new and i want that my Enemy would attack me and do a certain amount of damage (would be nice if there was a range and delay)
Here’s my Enemy script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAII : MonoBehaviour {
Transform playerTransform;
UnityEngine.AI.NavMeshAgent myNavmesh;
public float checkRate = 0.01f;
float nextCheck;
// Start is called before the first frame update
void Start() {
if (GameObject.FindGameObjectWithTag("Player").activeInHierarchy)
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
myNavmesh = gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
if (Time.time > nextCheck)
{
nextCheck = Time.time + checkRate;
FollowPlayer();
}
}
void FollowPlayer()
{
myNavmesh.transform.LookAt(playerTransform);
myNavmesh.destination = playerTransform.position;
}
}
And here’s my playerHealth script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
public HealthBar healthBar;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
// Update is called once per frame
void Update()
{
if // I don't know what to do here
{
TakeDamage(20);
}
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
}
So basicly i want the enemy do damage until my health bar is at 0.