Forum begins after the advertisement:


[Part 6] My charger enemy can’t walk

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 6] My charger enemy can’t walk

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #12259
    Raju Putra Dermawan
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    Charger.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Charger : Enemy
    {
        [SerializeField] private float ledgeCheckX;
        [SerializeField] private float ledgeCheckY;
        [SerializeField] private float chargeSpeedMultiplier;
        [SerializeField] private float chargeDuration;
        [SerializeField] private float jumpForce;
        [SerializeField] private LayerMask whatIsGround;
        float timer;
        // Start is called before the first frame update
    
        protected override void Start()
        {
            base.Start();
            ChangeState(EnemyStates.Charger_Idle);
            rb.gravityScale = 12f;
        }
    
        protected override void UpdateEnemyState()
        {
            if (health <= 0)
            {
                Death(0.05f);
            }
            Vector3 _ledgeCheckStart = transform.localScale.x > 0 ? new Vector3(ledgeCheckX, 0) : new Vector3(-ledgeCheckX, 0);
            Vector2 _wallCheckDir = transform.localScale.x > 0 ? transform.right : -transform.right;
    
            switch (GetCurrentEnemyState)
            {
                case EnemyStates.Charger_Idle: 
    
                    if (!Physics2D.Raycast(transform.position + _ledgeCheckStart, Vector2.down, ledgeCheckY, whatIsGround)
                        || Physics2D.Raycast(transform.position, _wallCheckDir, ledgeCheckX, whatIsGround))
                    {
                        transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
                    }
    
                    RaycastHit2D _hit = Physics2D.Raycast(transform.position + _ledgeCheckStart, _wallCheckDir, ledgeCheckX * 10);
    
                    if (_hit.collider != null && _hit.collider.gameObject.CompareTag("Player"))
                    {
                        ChangeState(EnemyStates.Charger_Surprissed);
                    }
    
                    break;
    
                case EnemyStates.Charger_Surprissed:
                    rb.velocity = new Vector2(0, jumpForce);
    
                    ChangeState(EnemyStates.Charger_Charge);
    
                    break;
    
                case EnemyStates.Charger_Charge:
                    timer += Time.deltaTime;
    
                    if(timer < chargeDuration)
                    {
                        if(Physics2D.Raycast(transform.position, Vector2.down, ledgeCheckY, whatIsGround))
                        {
                            if (transform.localScale.x > 0)
                            {
                                rb.velocity = new Vector2(speed * chargeSpeedMultiplier, rb.velocity.y);
                            }
                            else
                            {
                                rb.velocity = new Vector2(-speed * chargeSpeedMultiplier, rb.velocity.y);
                            }
                        }
                        else
                        {
                            rb.velocity = new Vector2(0, rb.velocity.y);
                        }
                    }
                    else
                    {
                        timer = 0;
                        ChangeState(EnemyStates.Charger_Idle);
                    }
    
                    break;
            }
        }
    
        protected override void ChangeCurrentAnimation()
        {
            if(GetCurrentEnemyState == EnemyStates.Charger_Idle)
            {
                anim.speed = 1;
            }
    
            if (GetCurrentEnemyState == EnemyStates.Charger_Charge)
            {
                anim.speed = chargeSpeedMultiplier;
            }
        }
    }
    

    My charger enemy cannot walk, but it can charge when the Player is in front of it

    My enemy can't walk

    View post on imgur.com

    #12261
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    The issue is probably because your currentEnemyState property is not updating correctly.

    Do you know how to enter Debug Mode on the Inspector? You can select your enemy in Debug Mode on the Inspector, and it will expose all private variables on the component as well. Once done, you can look at the currentEnemyState property and see if it is updating correctly as your enemy is going about its business.

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

Go to Login Page →


Advertisement below: