Forum begins after the advertisement:


[Part 6] Charger enemy rapidly turns left / right + not following player

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 6] Charger enemy rapidly turns left / right + not following player

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #18239
    Adit Rama
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    why does my charger enemy keep rapidly turns right/left, and does not chasing the player, it just jump suprised, and then chase the player in place :?

    this is my charger code

    
    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 once before the first execution of Update after the MonoBehaviour is created
        protected override void Start()
        {
            base.Start();
            ChangeStates(EnemyStates.Charger_Idle);
            rb.gravityScale = 12f;
        }
    
        protected override void UpdateEnemyStates()
        {
            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 (GetCurrentEnemyStates)
            {
                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"))
                    {
                        ChangeStates(EnemyStates.Charger_Suprised);
                    }
    
                    if (transform.localScale.x > 0)
                    {
                        rb.velocity = new Vector2(speed, rb.velocity.y);
                    }
                    else
                    {
                        rb.velocity = new Vector2(-speed, rb.velocity.y);
                    }
                    break;
    
                case EnemyStates.Charger_Suprised:
                    rb.velocity = new Vector2(0, jumpForce);
    
                    ChangeStates(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;
                        ChangeStates(EnemyStates.Charger_Idle);
                    }
                    break;
            }
        }
    
        protected override void ChangeCurrentAnimation()
        {
            if (GetCurrentEnemyStates == EnemyStates.Charger_Idle)
            {
                anim.speed = 1;
            }
    
            if (GetCurrentEnemyStates == EnemyStates.Charger_Charge)
            {
                anim.speed = chargeSpeedMultiplier;
            }
        }
    }```
    #18240
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Your Charger enemy may be rapidly turning right/left due to the logic in the Charger_Idle state where it checks for walls and ledges. If the raycasts detect a wall or no ground, it flips the scale, causing it to turn. Additionally, ensure that the speed variable is defined and set correctly, as it affects the horizontal movement.

    Could you confirm if the speed variable is properly initialized and if the raycasts are functioning as intended?

    #18241
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    @aditrama can you post your Enemy code as well?

    If you can post a video recording of the issue, it will help a lot as well.

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

Go to Login Page →


Advertisement below: