Search Unity

Question My Animation for the enemy to attack will not work

Discussion in 'Scripting' started by Senseless_230, Dec 10, 2022.

Thread Status:
Not open for further replies.
  1. Senseless_230

    Senseless_230

    Joined:
    Aug 1, 2022
    Posts:
    36
    The condition regarding the "Enemy Attacking does not work and i am very confused why.

    Attack is a Trigger Parameter and yet it wont work when being called
    upload_2022-12-10_1-54-30.png


    Enemy CODE
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.InputSystem;
    7.  
    8. public class Enemy : character
    9. {
    10.     Animator animator;
    11.  
    12.  
    13.     public float collisionOffset = 0.05f;
    14.  
    15.     [SerializeField]
    16.     private float moveSpeed = 5f;
    17.  
    18.     SpriteRenderer spriteRenderer;
    19.  
    20.  
    21.     private Vector3 startingPosition;
    22.  
    23.    LevelSystem levelSystem = new LevelSystem();
    24.    
    25.  
    26.  
    27.     Rigidbody2D rb;
    28.   private  Transform target;
    29.  
    30.     private IState currentState;
    31.  
    32.     public float MyAttackRange { get; set; }
    33.  
    34.     public float MyAttackTime { get; set; }
    35.  
    36.     public Transform Target
    37.     {
    38.         get
    39.         {
    40.             return target;
    41.         }
    42.         set
    43.         {
    44.             target = value;
    45.         }
    46.     }
    47.  
    48.  
    49.  
    50.     Vector2 moveDirection;
    51.  
    52.  
    53.     protected void Awake()
    54.     {
    55.         MyAttackRange = 1;
    56.         ChangeState(new IdleState());
    57.         rb = GetComponent<Rigidbody2D>();
    58.        spriteRenderer = GetComponent<SpriteRenderer>();
    59.  
    60.     }
    61.  
    62.  
    63.  
    64.    public void EnemyCollisions()
    65.     {
    66.  
    67.     }
    68.  
    69.     public float Health
    70.     {
    71.         set
    72.         {
    73.             // prints the amount of health set to the enemy
    74.             print(value);
    75.             health = value;
    76.             Debug.Log("This is the health of the enemy" + health); // this tells unity to paste the fact that this code is being called
    77.             if (health <= 0)
    78.             {
    79.  
    80.  
    81.                 Defeated();
    82.                
    83.             }
    84.         }
    85.         get
    86.         {
    87.             return health;
    88.         }
    89.     }
    90.  
    91.     public float MoveSpeed { get => moveSpeed; set => moveSpeed = value; }
    92.  
    93.  
    94.  
    95.     // allows the health to be changed in unity
    96.     public float health = 1;
    97.  
    98.  
    99.    
    100.  
    101.  
    102.     private void Start()
    103.     {
    104.  
    105.         startingPosition = transform.position;
    106.         // grabs the animator from the scene
    107.         animator = GetComponent<Animator>();
    108.         // no need for this due to the fact that we have a new follow target script  :)
    109.    //     target = GameObject.Find("Player").transform;
    110.         rb = GetComponent<Rigidbody2D>();
    111.     }
    112.  
    113.     //private Vector3 GetRoamingPosition()
    114.     //{
    115.  
    116.     //}
    117.  
    118.  
    119.    
    120.  
    121.     protected override void Update()
    122.     {
    123.         if (!IsAttacking)
    124.         {
    125.             MyAttackTime += Time.deltaTime;
    126.         }
    127.        
    128.         currentState.Update();
    129.     }
    130.              
    131.  
    132.  
    133.     public void Defeated()
    134.     {
    135.         // calls the animation that is set as an event in unity
    136.         ScoreManager.instance.AddPoint();
    137.         MMScoreManager.instance.AddPoint();
    138.         Debug.Log("Enemy  :  Defeated");
    139.         animator.SetTrigger("Defeated");
    140.         Destroy(gameObject);
    141.         Debug.Log(levelSystem);
    142.        
    143.         levelSystem.AddExperience();
    144.      
    145.        
    146.        
    147.  
    148.        
    149.  
    150.         // add experience points on death
    151.  
    152.     }
    153.  
    154.  
    155.    
    156.  
    157.     public void RemoveEnemy()
    158.     {
    159.  
    160.         // d e a t h
    161.         Defeated();
    162.         // self explnanatory, destroys the game object this code is linked to, specifically the enemy
    163.         Destroy(gameObject);
    164.         Debug.Log("Yes this works : " + gameObject); // makes sure that this code is being called.
    165.  
    166.     }
    167.  
    168.     private IEnumerator Attack()
    169.     {
    170.         MyAnimator.SetBool("attack", true);
    171.  
    172.         IsAttacking = true;
    173.  
    174.         yield return new WaitForSeconds(3);
    175.  
    176.        
    177.     }
    178.  
    179.  
    180.     public void ChangeState(IState newState)
    181.     {
    182.         if (currentState != null)
    183.         {
    184.             currentState.Exit();
    185.         }
    186.  
    187.         currentState = newState;
    188.  
    189.         currentState.Enter(this);
    190.     }
    191.  
    192. }
    193.  
    194.  
    AND ATTACK STATE CODE
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class AttackState : IState
    7. {
    8.  
    9.     // reference to states parent
    10.     private Enemy parent;
    11.     private float attackCooldown = 3;
    12.  
    13.  
    14.     private float extraRange = .1f;
    15.  
    16.     public void Enter(Enemy parent)
    17.     {
    18.         this.parent = parent;
    19.     }
    20.  
    21.     public void Exit()
    22.     {
    23.     }
    24.  
    25.     public void Update()
    26.     {
    27.        
    28.         Debug.Log("The Enemy is attacking you...");
    29.  
    30.  
    31.  
    32.         if (parent.MyAttackTime >= attackCooldown && !parent.IsAttacking)
    33.         {
    34.             parent.MyAttackTime = 0;
    35.  
    36.             parent.StartCoroutine(Attack());
    37.         }
    38.      
    39.      
    40.  
    41.         if(parent.Target != null)
    42.         {
    43.             float distance = Vector2.Distance(parent.Target.position, parent.transform.position);
    44.  
    45.             if(distance >= parent.MyAttackRange+extraRange && !parent.IsAttacking)
    46.             {
    47.                 parent.ChangeState(new FollowState());
    48.             }
    49.             //check range and then attack
    50.  
    51.         }
    52.         else
    53.         {
    54.             parent.ChangeState(new IdleState());
    55.         }
    56.     }
    57.  
    58.     public IEnumerator Attack()
    59.     {
    60.         parent.IsAttacking = true;
    61.  
    62.  
    63.         parent.MyAnimator.SetTrigger("attack");
    64.  
    65.         yield return new WaitForSeconds(parent.MyAnimator.GetCurrentAnimatorStateInfo(1).length);
    66.  
    67.         parent.IsAttacking = false;
    68.  
    69.  
    70.  
    71.     }
    72.  
    73. }
    74.  
    75.  

    AND THE CHARACTER INHERITANCE THEY ARE BOTH INHERITING FROM
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public abstract class character : MonoBehaviour
    7. {
    8.  
    9.     protected float speed;
    10.  
    11.     private Rigidbody2D myRigidbody;
    12.  
    13.     public Animator MyAnimator { get; set; }
    14.  
    15.    
    16.  
    17.     protected Vector2 direction;
    18.  
    19.  
    20.     private void Start()
    21.     {
    22.         MyAnimator = GetComponent<Animator>();
    23.     }
    24.  
    25.  
    26.     public bool IsAttacking { get; set; }
    27.    
    28.     public bool isMoving
    29.     {
    30.         get
    31.         {
    32.             return direction.x != 0 || direction.y != 0;
    33.         }
    34.     }
    35.  
    36.    protected virtual void Update()
    37.     {
    38.         HandleLayers();
    39.     }
    40.  
    41.  
    42.     public void Move()
    43.     {
    44.         myRigidbody.velocity = direction.normalized * speed;
    45.  
    46.     }
    47.  
    48.  
    49.     public void HandleLayers()
    50.     {
    51.         if (isMoving)
    52.         {
    53.            
    54.         }
    55.         else if (IsAttacking)
    56.         {
    57.             ActivateLayer("AttackLayer");
    58.         }
    59.         else
    60.         {
    61.             ActivateLayer("IdleLayer");
    62.         }
    63.     }
    64.  
    65.     public void ActivateLayer(string layerName)
    66.     {
    67.         for (int i = 0; i < MyAnimator.layerCount; i++)
    68.         {
    69.             MyAnimator.SetLayerWeight(i, 0);
    70.         }
    71.  
    72.         MyAnimator.SetLayerWeight(MyAnimator.GetLayerIndex(layerName), 1);
    73.     }
    74. }
    75.  
    please help me fix this..

    please and thank yous
     
  2. Senseless_230

    Senseless_230

    Joined:
    Aug 1, 2022
    Posts:
    36



    just as a little update i found that there is a Null exception error, still confusing to find how to fix however
    upload_2022-12-10_2-18-10.png

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class AttackState : IState
    7. {
    8.  
    9.     // reference to states parent
    10.     private Enemy parent;
    11.     private float attackCooldown = 3;
    12.  
    13.  
    14.     private float extraRange = .1f;
    15.  
    16.     public void Enter(Enemy parent)
    17.     {
    18.         this.parent = parent;
    19.     }
    20.  
    21.     public void Exit()
    22.     {
    23.     }
    24.  
    25.     public void Update()
    26.     {
    27.      
    28.    
    29.  
    30.  
    31.  
    32.         if (parent.MyAttackTime >= attackCooldown && !parent.IsAttacking)
    33.         {
    34.             parent.MyAttackTime = 0;
    35.    Debug.Log("The Enemy is attacking you...");
    36.             parent.StartCoroutine(Attack());
    37.  
    38.         }
    39.  
    40.  
    41.  
    42.         if (parent.Target != null)
    43.         {
    44.             float distance = Vector2.Distance(parent.Target.position, parent.transform.position);
    45.  
    46.             if(distance >= parent.MyAttackRange+extraRange && !parent.IsAttacking)
    47.             {
    48.                 parent.ChangeState(new FollowState());
    49.             }
    50.             //check range and then attack
    51.  
    52.         }
    53.         else
    54.         {
    55.             parent.ChangeState(new IdleState());
    56.         }
    57.     }
    58.  
    59.     public IEnumerator Attack()
    60.     {
    61.         parent.IsAttacking = true;
    62.  
    63.  
    64.         parent.MyAnimator.SetTrigger("attack");
    65.  
    66.         yield return new WaitForSeconds(parent.MyAnimator.GetCurrentAnimatorStateInfo(1).length);
    67.  
    68.         parent.IsAttacking = false;
    69.  
    70.  
    71.  
    72.     }
    73.  
    74. }
    75.  
    76.  
    63 is the issue apparently.[/CODE]
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Wat?! I already TOLD you this here:

    https://forum.unity.com/threads/someone-help-me-fix-this-please.1357901/#post-8566793

    There's only ONE answer, and it's ALWAYS the same!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    It's a null error. Fix the error.
     
Thread Status:
Not open for further replies.