Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Attack Trigger

Discussion in 'Scripting' started by Milambur88, Jun 2, 2020.

  1. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    i am getting an issue where i cant get my enemy to play the attack animation.

    I am following a tutorial which said to me that this may happen but in no way explained how to fix it.

    to me it seems it just continues with the walk animation.

    I have added a cylinder around my FP controler which is set to be a trigger.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpiderAI : MonoBehaviour
    6. {
    7.     public GameObject ThePlayer;
    8.     public float TargetDistance;
    9.     public float AllowedRange = 30;
    10.     public GameObject TheEnemy;
    11.     public float EnemySpeed;
    12.     public int AttackTrigger;
    13.     public RaycastHit Shot;
    14.  
    15.     void Update()
    16.     {
    17.         transform.LookAt(ThePlayer.transform);
    18.         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
    19.         {
    20.             TargetDistance = Shot.distance;
    21.             if (TargetDistance <= AllowedRange)
    22.             {
    23.                 EnemySpeed = 0.05f;
    24.                 if (AttackTrigger == 0)
    25.                 {
    26.                     TheEnemy.GetComponent<Animation>().Play("walk");
    27.                     transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed);
    28.                 }
    29.             }
    30.             else
    31.             {
    32.                 EnemySpeed = 0;
    33.                 TheEnemy.GetComponent<Animation>().Play("idle");
    34.             }
    35.         }
    36.         if (AttackTrigger == 1)
    37.         {
    38.             EnemySpeed = 0;
    39.             TheEnemy.GetComponent<Animation>().Play("attack");
    40.         }
    41.     }
    42.     void OnTriggerEnter()
    43.     {
    44.         AttackTrigger = 1;
    45.     }
    46.  
    47.     void OnTriggerExit()
    48.     {
    49.         AttackTrigger = 0;
    50.     }
    51. }
     
  2. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
  3. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    Thanks , is this the only way using the Animator its just every video i have watched to do with animation and unity they all say disable animator and use animation for better control?
     
  4. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
  5. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
  6. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    Thanks for the links, my issue is i'm not getting a collision with the enemy i have followed the setup on the first link and i still don't get any collision detection
     
  7. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Did
    Did you change your code if you don't mind I can have a look
     
    Milambur88 likes this.
  8. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    And do player and the enemy both have a collider and 1 of them with a rigibody
    And which one the the trigger collider
     
    Milambur88 likes this.
  9. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    I have got it the enemy didn't have a rigid body assigned to it. thanks this has been really helpful!!!
     
  10. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    My updated code as well thanks


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpiderAI : MonoBehaviour
    6. {
    7.     public GameObject ThePlayer;
    8.     public float TargetDistance;
    9.     public float AllowedRange = 30;
    10.     public GameObject TheEnemy;
    11.     public float EnemySpeed;
    12.     public int AttackTrigger;
    13.     public RaycastHit Shot;
    14.  
    15.     void Update()
    16.     {
    17.         transform.LookAt(ThePlayer.transform);
    18.         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
    19.         {
    20.             TargetDistance = Shot.distance;
    21.             if (TargetDistance <= AllowedRange)
    22.             {
    23.                 EnemySpeed = 0.05f;
    24.                 if (AttackTrigger == 0)
    25.                 {
    26.                     TheEnemy.GetComponent<Animation>().Play("walk");
    27.                     transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed);
    28.                 }
    29.             }
    30.             else
    31.             {
    32.                 EnemySpeed = 0;
    33.                 TheEnemy.GetComponent<Animation>().Play("idle");
    34.             }
    35.         }
    36.         if (AttackTrigger == 1)
    37.         {
    38.             EnemySpeed = 0;
    39.             TheEnemy.GetComponent<Animation>().Play("attack");
    40.         }
    41.     }
    42.     void OnTriggerEnter(Collider other)
    43.     {
    44.         AttackTrigger = 1;
    45.         Debug.Log("I hit you");
    46.     }
    47.  
    48.     void OnTriggerExit(Collider other)
    49.     {
    50.         AttackTrigger = 0;
    51.     }
    52. }
     
  11. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    And on trigger might as well check which collider been trigger..cause it could been trigger by other things too
     
  12. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    i now have the problem that my damage dealt script isn't actually dealing damage using debug.log i can see it now detects the sword swing collision as also the enemy collision.
     
  13. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    This is my damage script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class InflictDamage : MonoBehaviour
    6. {
    7.     public int DamageDealt = 5;
    8.     public float TargetDistance;
    9.     public float AllowedRange = 2.7f;
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         if (Input.GetButtonDown("Fire1"))
    15.         {
    16.             RaycastHit hit;
    17.             if (Physics.Raycast(transform.position, transform.TransformDirection (Vector3.forward), out hit))
    18.             {
    19.                 TargetDistance = hit.distance;
    20.                 if (TargetDistance <= AllowedRange)
    21.                 {
    22.                     Debug.Log("Take 5 Damage");
    23.                     hit.transform.SendMessage("DeductPoints", DamageDealt, SendMessageOptions.DontRequireReceiver);
    24.                 }
    25.             }
    26.         }
    27.     }
    28.  
    29. }
    This is the enemy script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpiderEnemy : MonoBehaviour
    6. {
    7.     public int EnemyHealth = 10;
    8.     public GameObject TheSpider;
    9.     public int SpiderStatus;
    10.     public int BaseXP = 10;
    11.     public int CalculatedXP;
    12.     public SpiderAI SpiderAIScript;
    13.  
    14.     void start()
    15.     {
    16.         SpiderAIScript = GetComponent<SpiderAI>();
    17.     }
    18.  
    19.     void DeductPoints (int DamageAmount)
    20.     {
    21.         EnemyHealth -= DamageAmount;
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.        
    28.         if (EnemyHealth <= 0 ){
    29.             if (SpiderStatus == 0)
    30.             {
    31.  
    32.                 StartCoroutine(DeathSpider());
    33.  
    34.             }
    35.         }
    36.     }
    37.  
    38.     IEnumerator DeathSpider()
    39.     {
    40.         SpiderAIScript.enabled = false;
    41.         SpiderStatus = 6;
    42.         CalculatedXP = BaseXP * GlobalLevel.CurrentLevel;
    43.         GlobalXP.CurrentXP += CalculatedXP;
    44.         yield return new WaitForSeconds(0.8f);
    45.         TheSpider.GetComponent<Animation>().Play("die");
    46.     }
    47. }
    48.  
     
  14. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Code (CSharp):
    1. void DeductPoints (int DamageAmount)
    2.     {
    3.         EnemyHealth -= DamageAmount;
    4.         if (EnemyHealth <= 0 ){
    5.             if (SpiderStatus == 0)
    6.             {
    7.                 StartCoroutine(DeathSpider());
    8.             }
    9. }
    10.        
    then you won't have to check health on every frame . as for the damagescript best to check what have you hit
     
    Milambur88 likes this.
  15. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    Okay thanks for all your help it is now working it walks it attacks and it dies
     
  16. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Nice