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 antnoije · Jan 07, 2018 at 03:55 AM · playeraidamageyieldyield waitforseconds

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.

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 antnoije · Jan 07, 2018 at 03:57 AM 0
Share

When the player gets in the attack range of the enemy, the player takes a lot of damage in very little time.

avatar image CBRZY · Jan 07, 2018 at 01:24 PM 0
Share

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()

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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()

Comment
Add comment · 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
0

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.

Comment
Add comment · 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

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

132 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

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

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


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