Why does my player character take damage instantly, i have an IEnumerator?
This is my AI script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AnotherAI : MonoBehaviour {
public GameObject ScreenFlash;
public GameObject playerObject;
public AudioSource Hurt01;
public AudioSource Hurt02;
public AudioSource Hurt03;
public float lookRadius = 10f;
public float attackRadius = 2f;
public int Damage = 1;
private int PainSound;
Transform target;
NavMeshAgent agent;
void Start () {
target = PlayerManager.instance.player.transform;
agent = GetComponent<NavMeshAgent> ();
}
void Update () {
float distance = Vector3.Distance (target.position, transform.position);
if (distance <= lookRadius) {
agent.SetDestination (target.position);
if (distance <= attackRadius) {
FaceTarget();
StartCoroutine (Attack ());
}
}
}
IEnumerator Attack() {
PainSound = Random.Range (1, 4);
yield return new WaitForSeconds (4f);
ScreenFlash.SetActive (true);
PlayerStatsScript PS = playerObject.GetComponent<PlayerStatsScript> ();
PS.HP -= Damage;
if (PainSound == 1) {
Hurt01.Play ();
}
if (PainSound == 2) {
Hurt02.Play ();
}
if (PainSound == 3) {
Hurt03.Play ();
}
yield return new WaitForSeconds (4f);
ScreenFlash.SetActive (false);
}
void FaceTarget (){
Vector3 direction = (target.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);
}
void OnDrawGizmosSelected (){
Gizmos.color = Color.red;
Gizmos.DrawWireSphere (transform.position, lookRadius);
}
}
This is my player stats script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerStatsScript : MonoBehaviour {
public int HP = 100;
public void TakeDamage (float amount)
{
if (HP <= 0f)
{
Dead ();
}
}
void Dead ()
{
Debug.Log ("Game Over");
}
}
This is my first time posting a question hopefully someone will know how to help me, either way thanks for trying.
When the player gets in the attack range of the enemy, the player takes a lot of damage in very little time.
I see you have accepted an answer. But the actual problem is that you are starting a Coroutine in Update() which is causing problems. You have stated that the Coroutine should only happen every 4 seconds, but Update() is calling the Coroutine every frame.
You should never call a Coroutine in Update, but rather in Start()
Answer by CBRZY · Jan 07, 2018 at 03:55 PM
The problem is that you are starting a Coroutine in Update() which is causing problems. You have stated that the Coroutine should only happen every 4 seconds, but Update() is calling the Coroutine every frame.
You should never call a Coroutine in Update, but rather in Start()
Answer by adriant · Jan 08, 2018 at 09:40 AM
I think you can call the Attack coroutine during Update() but that would cause it to be called every frame. Maybe what you would want to do would be to add some bool m_IsAttacking;
flag that is set to true
at the beginning of Attack()
and to false
at the end of the Attack()
and don't start the attack again if m_IsAttacking == true
.
Your answer
Follow this Question
Related Questions
Invoke method is not called 2 Answers
WaitForSeconds clarification 1 Answer
Damage Players Using Raycast 0 Answers
Critical Strike Damage (Making the UI text bigger when critical strike happens) 0 Answers