Forum begins after the advertisement:

 


[Part 4] Cast and Heal problem

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 4] Cast and Heal problem

Viewing 7 posts - 21 through 27 (of 27 total)
  • Author
    Posts
  • #16177
    pepecry
    Level 7
    Participant
    Helpful?
    Up
    0
    ::

    Ah yes work again. Must be mine mistaken at sometime when making other function. But can I ask for why my wolf after get hit, he stuck on stunned animation but not changing to run animation even when I was stand front of him, like in the video. I make the stunned like the fly enemy guide. Is that the reason? What should I modify?

    wolf stun wolf stun video here is my code for the wolf
    <code>using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class WolfSpririt : Enemy
    {
        [SerializeField] private float flipWaittime;
        [SerializeField] private float ledgecheckX;
        [SerializeField] private float ledgecheckY;
        [SerializeField] private float chargeSpeed;
        [SerializeField] private float chargeDuration;
        [SerializeField] private float jumpForce;
        [SerializeField] private float stunDuration;
        [SerializeField] private LayerMask whatIsGround;
        float timer;
        float stunTimer;
        // Start is called before the first frame update
    
        protected override void Start()
        {
            base.Start();
            rb.gravityScale = 12.0f;
            ChangeState(EnemyStates.Wolf_Idle);
        }
    
        protected override void Death(float _destroyTime)
        {
            rb.gravityScale = 12;
            base.Death(_destroyTime);
        }
    
        // Update is called once per frame
        protected override void UpdateEnemyStates()
        {
            if (health <= 0)
            {
                Death(0.05f);
            }
            Vector3 _ledgeCheckStartPoint = 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.Wolf_Idle:
    
                    if (!Physics2D.Raycast(transform.position + _ledgeCheckStartPoint, Vector2.down, ledgecheckY, whatIsGround) || Physics2D.Raycast(transform.position,
                        _wallCheckDir, ledgecheckX, whatIsGround))
                    {
                        ChangeState(EnemyStates.Wolf_Alert);
                    }
    
                    RaycastHit2D _hit = Physics2D.Raycast(transform.position + _ledgeCheckStartPoint, _wallCheckDir, ledgecheckX * 10);
                    if (_hit.collider != null && _hit.collider.gameObject.CompareTag("Player"))
                    {
                        ChangeState(EnemyStates.Wolf_Suprise);
                    }
    
                    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.Wolf_Alert:
                    timer += Time.deltaTime;
                    if (timer > flipWaittime) 
                    {
                        timer = 0;
                        transform.localScale = new Vector2(transform.localScale.x * -1,transform.localScale.y);
                        ChangeState(EnemyStates.Wolf_Idle);
                    }
    
                    break;
                case EnemyStates.Wolf_Suprise:
                    rb.velocity = new Vector2(0, jumpForce);
                    ChangeState (EnemyStates.Wolf_Charge);
                    break;
                case EnemyStates .Wolf_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 * chargeSpeed, rb.velocity.y);
                            }
                            else
                            {
                                rb.velocity = new Vector2(-speed * chargeSpeed, rb.velocity.y);
                            }
    
                        }
                        else
                        {
                            rb.velocity = new Vector2(0,rb.velocity.y);
                        }
                    }
                    else
                    {
                        timer = 0;
                        ChangeState(EnemyStates.Wolf_Idle);
                    }
                    break;
                case EnemyStates.Wolf_Stun:
                    stunTimer += Time.deltaTime;
                    if (stunTimer > stunDuration)
                    {
                        ChangeState(EnemyStates.Wolf_Idle);
                        stunTimer = 0;
                    }
                    break;
                case EnemyStates.Wolf_Dead:
                    Death(Random.Range(1, 3));
                    break;
            }
    
        }
    
        public override void EnemyHit(float _DmgDone, Vector2 _hitDirection, float _hitForce)
        {
            base.EnemyHit(_DmgDone, _hitDirection, _hitForce);
            if (health > 0)
            {
                ChangeState(EnemyStates.Wolf_Stun);
            }
            else
            {
                ChangeState(EnemyStates.Wolf_Dead);
            }
        }
    
        protected override void ChangeCurrentAnimation()
        {
            anim.SetBool("Patrol", GetCurrentEnemyStates == EnemyStates.Wolf_Idle);
            anim.SetBool("Found", GetCurrentEnemyStates == EnemyStates.Wolf_Suprise);
            anim.SetBool("Stunned", GetCurrentEnemyStates == EnemyStates.Wolf_Stun);
            if (GetCurrentEnemyStates == EnemyStates.Wolf_Idle)
            {
                anim.speed = 1;
            }
    
            if (GetCurrentEnemyStates == EnemyStates.Wolf_Charge)
            {
                anim.speed = chargeSpeed;
            }
            if (GetCurrentEnemyStates == EnemyStates.Wolf_Dead)
            {
                anim.SetTrigger("Death");
                int DeadCorpse = LayerMask.NameToLayer("DeadCorpse");
            }
        }
    }
    </code>

    Tks for your help

    #16178
    Chloe Lim
    Level 12
    Moderator
    Helpful?
    Up
    0
    ::

    Can you send a screenshot of how the inspector of your transition arrow from stun to run looks like?

    #16180
    pepecry
    Level 7
    Participant
    Helpful?
    Up
    0
    ::

    Here is the inspector

    Wolf stun to run transition
    #16190
    Chloe Lim
    Level 12
    Moderator
    Helpful?
    Up
    0
    ::

    Ok, i think you first have to reset the normal timer back to 0 when the wolf is stunned,

    <code>                if (stunTimer > stunDuration)
                    {
                        ChangeState(EnemyStates.Wolf_Idle);
                        stunTimer = 0;
                        timer = 0;
    
                    }</code>

    because when the wolf goes back to idle, the timer is still increasing, and the requirement for if (timer < chargeDuration) might not be met in the charge state also try changing Changecurrentanimation to make the Found bool be active during wolf charge instead anim.SetBool("Found", GetCurrentEnemyStates == EnemyStates.Wolf_Charge); Let me know if it works

    #16191
    pepecry
    Level 7
    Participant
    Helpful?
    Up
    0
    ::

    So I change as you said. The animation is back to the run perfectly but for some reason, the stunned animation just didn’t run. Like in the record here

    Stun animation Stun animation just hit and freeze then change back to run The code as u said I change it to this anim.SetBool("Found", GetCurrentEnemyStates == EnemyStates.Wolf_Charge);
    #16194
    Chloe Lim
    Level 12
    Moderator
    Helpful?
    Up
    0
    ::

    Actually the stun should be that way, our bat enemy behaves the same way as well

    #16195
    pepecry
    Level 7
    Participant
    Helpful?
    Up
    0
    ::

    Ok. Thank for your help

Viewing 7 posts - 21 through 27 (of 27 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: