Forum begins after the advertisement:


[Part 3] Problem with my Zombie enemy script

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 3] Problem with my Zombie enemy script

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #13052
    Kristine
    Silver Supporter (Patron)

    So, not sure what my issue is but after my player hits my zombie player the third time (first hit and second hit are fine!), my zombie just goes backwards. I wrote this also on the video comments on YouTube.

    Here’s my enemy script. I’ve tried playing around with it to get this to stop. And I am just at a loss:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Enemy : MonoBehaviour
    {
        [SerializeField] protected Animator animator;
    
        [Header("Enemy Stats")]
        [SerializeField] protected float health;
        [SerializeField] protected float speed;
        [SerializeField] protected float attackDamage;
    
        [Header("Enemy Recoil")]
        [SerializeField] protected float recoilLength;
        [SerializeField] protected float recoilFactor;
        [SerializeField] protected bool isRecoiling = false;
        protected float recoilTimer;
    
        protected Rigidbody2D rb;
        protected bool takenDamage = false; 
    
        protected virtual void Awake()
        {
            rb = GetComponent<Rigidbody2D>();
        }
    
        // Update is called once per frame
        protected virtual void Update()
        {
            takenDamage = false; //every frame sets the takenDamage bool to false
            Death();
            Recoil();
        }
    
        protected virtual void Recoil()
        {
            //Recoils the Enemy
            if (isRecoiling)
            {
                if(recoilTimer < recoilLength)
                {   recoilTimer += Time.deltaTime; }
                else
                {
                    isRecoiling = false;
                    recoilTimer = 0; 
                }
            }
        }
    
        public virtual void EnemyHit(float damageDone, Vector2 hitDir, float hitForce) 
        {
            if (takenDamage) return; 
    
            //if Enemy is hit, damage is done and recoils from hit
            health -= damageDone;
            animator.SetTrigger("Hit"); 
    
            if(!isRecoiling )
            {
                isRecoiling = true;
                rb.AddForce(-hitForce * recoilFactor * hitDir); 
    
            }
            takenDamage = true; 
        
        }
    
        protected void OnTriggerStay2D(Collider2D other)
        {
            if (other.CompareTag("Player") && !(PlayerController.Instance.pState.invincible)) { Attack(); }
        }
    
        protected virtual void Attack()
        {
            PlayerController.Instance.TakeDamage(attackDamage);
            isRecoiling = true; 
        }
    
        protected virtual void Death()
        {
            //When enemy loses all health, they will be destroyed
            if (health <= 0) { Destroy(gameObject); }
        }
    }

    And Here is my Zombie Code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Zombie : Enemy
    {
        [SerializeField] private GameObject player; 
        
        protected override void Awake()
        {
            rb = GetComponentInParent<Rigidbody2D>(); //My Zombie GameOBject is in my Main Enemy gameObject for organization purposes. 
        }
    
        private void Start()
        {
            rb.gravityScale = 12f;
            
        }
    
        protected override void Update()
        {
            base.Update();
            Flip(); 
            ChasePlayer();  
        }
    
        public override void EnemyHit(float damageDone, Vector2 hitDir, float hitForce)
        {
            base.EnemyHit(damageDone, hitDir, hitForce);
        }
    
        protected void ChasePlayer()
        {
            
            if (!isRecoiling)
            {
                transform.position = Vector2.MoveTowards(transform.position, new Vector2(player.transform.position.x, transform.position.y), speed * Time.deltaTime);
                animator.SetInteger("State", 1);
                animator.SetBool("IsZombie", true);
            }
        }
    
        protected void Flip()
        {
            if (player.transform.position.x < transform.position.x)
            {
                transform.localScale = new Vector2(-1, transform.localScale.y);
            }
            else if (player.transform.position.x >= transform.position.x)
            {
                transform.localScale = new Vector2(1, transform.localScale.y);
            }
        }
    }
    #13061
    Terence
    Keymaster

    Hi Kristine, thanks for making the post.

    Can you show me a recording of this happening? If you know how to, select your Zombie in the recording as well with the Rigidbody exposed on the Inspector so I can have a look.

    #13066
    Kristine
    Silver Supporter (Patron)

    I actually fixed it by making a new enemy and setting it up. Not sure why the first enemy caused issues, but it now works. Thanks for the tutorials. They are great!

    #13072
    Terence
    Keymaster

    No problem Kristine. Thanks for your support!

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: