Forum begins after the advertisement:
[Part 10] Boss hit very fast and Null ref
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 10] Boss hit very fast and Null ref
- This topic has 1 reply, 1 voice, and was last updated 7 hours, 44 minutes ago by pepecry.
-
AuthorPosts
-
November 18, 2024 at 3:33 am #16384::
So i was making the boss for the game. And when I making the boss event and test the 3 hit function. My boss hit very fast. As we could said it doesn’t reset the animator. It just 2 punch in a frame. And when the boss hit, the console send a message of NullReference. As you can see in what i show below. First is the test video
Video test boss
then is the code. First is the CorruptedMonk.csusing System.Collections; using System.Collections.Generic; using UnityEngine; public class CorruptedMonk : Enemy { public static CorruptedMonk Instance; [Header("Attacking setting")] [SerializeField] public Transform sideAttackTransform, upAttackTransform, downAttackTransform; [SerializeField] public Vector2 sideAttackArea, upAttackArea, downAttackArea; [Space(5)] public float attackRange; public float attackTimer; [Header("Groundcheck setting")] [SerializeField] private Transform groundcheck; [SerializeField] private float groundchecky = 0.2f; [SerializeField] private float groundcheckx = 0.5f; [SerializeField] private LayerMask isground; int hitCounter; bool stunned, canStunned; bool alive; [HideInInspector] public float runSpeed; [HideInInspector] public bool facingRight; private void Awake() { if(Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } } // Start is called before the first frame update protected override void Start() { base.Start(); Sr = GetComponentInChildren<SpriteRenderer>(); anim = GetComponentInChildren<Animator>(); ChangeState(EnemyStates.Monk_State1); alive = true; } public bool isonGround() { if (Physics2D.Raycast(groundcheck.position, Vector2.down, groundchecky, isground) || Physics2D.Raycast(groundcheck.position + new Vector3(groundcheckx, 0, 0), Vector2.down, groundchecky, isground) || Physics2D.Raycast(groundcheck.position + new Vector3(-groundcheckx, 0, 0), Vector2.down, groundchecky, isground) ) { return true; } else { return false; } } private void OnDrawGizmos() { Gizmos.color = Color.yellow; Gizmos.DrawWireCube(sideAttackTransform.position, sideAttackArea); Gizmos.DrawWireCube(upAttackTransform.position, upAttackArea); Gizmos.DrawWireCube(downAttackTransform.position, downAttackArea); } // Update is called once per frame protected override void Update() { base.Update(); if (!attacking) { attackCountdown -= Time.deltaTime; } } public void Flip() { if (Move.Instance.transform.position.x < transform.position.x && transform.localScale.x > 0) { transform.eulerAngles = new Vector2(transform.eulerAngles.x, 180); facingRight = false; } else { transform.eulerAngles = new Vector2(transform.eulerAngles.x, 0); facingRight = true; } } protected override void UpdateEnemyStates() { if(Move.Instance != null) { switch (GetCurrentEnemyStates) { case EnemyStates.Monk_State1: break; case EnemyStates.Monk_State2: break; case EnemyStates.Monk_State3: break; case EnemyStates.Monk_State4: break; } } } protected override void OnCollisionStay2D(Collision2D _other) { } public void ResetAllAttack() { attacking = false; StopCoroutine(TriplePunch()); } #region attacking #region variables [HideInInspector] public bool attacking; [HideInInspector] public float attackCountdown; #endregion #region Control public void AttackHandler() { if(currentState == EnemyStates.Monk_State1) { if(Vector2.Distance(Move.Instance.transform.position,rb.position) <= attackRange) { StartCoroutine(TriplePunch()); } else { return; } } } #endregion #region Stage1 IEnumerator TriplePunch() { attacking = true; rb.velocity = Vector2.zero; anim.SetTrigger("Punch"); yield return new WaitForSeconds(0.3f); anim.ResetTrigger("Punch"); anim.SetTrigger("Punch"); yield return new WaitForSeconds(0.5f); anim.ResetTrigger("Punch"); anim.SetTrigger("Punch"); yield return new WaitForSeconds(0.1f); anim.ResetTrigger("Punch"); ResetAllAttack(); } #endregion #endregion }
Then is the Corrupted_Monk_Event.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Corrupted_Monk_Event : MonoBehaviour { void PunchDamagePlayer() { if (Move.Instance.transform.position.x > transform.position.x || Move.Instance.transform.position.x < transform.position.x) { Hit(CorruptedMonk.Instance.sideAttackTransform, CorruptedMonk.Instance.sideAttackArea); } else if (Move.Instance.transform.position.y > transform.position.y) { Hit(CorruptedMonk.Instance.upAttackTransform, CorruptedMonk.Instance.upAttackArea); } else if (Move.Instance.transform.position.y < transform.position.y) { Hit(CorruptedMonk.Instance.downAttackTransform, CorruptedMonk.Instance.downAttackArea); } } void Hit(Transform _attackTrasnform, Vector2 _attackArea) { Collider2D[] _objectToHit = Physics2D.OverlapBoxAll(_attackTrasnform.position, _attackArea, 0); for(int i = 0 ; i < _objectToHit.Length; i++) { if (_objectToHit[i].GetComponent<Collider2D>() != null) { _objectToHit[i].GetComponent<Move>().takeDmg(CorruptedMonk.Instance.dmgMake); } } } }
I even try to change from WaitForSecond to WaitForSecondRealTime but still doesn’t making the punch slow down.
Tks you for the series. And another thank for helping meNovember 18, 2024 at 3:53 am #16385::Update: I add a debug to check how many punch the boss does and it only make 3 punch. So the Boss still hitting 3 hit. But i got 6 damage and the boss doesn’t need to reset animation at all
and the null ref i already fix it. The getcomponent at the event first getcomponent i just took it wrong. From PlayerController to collider2d. It my fault. It kinda late in the time I made it so maybe my eyes has lied me. -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: