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 12 posts - 16 through 27 (of 27 total)
  • Author
    Posts
  • #15931
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    Ah alright, after taking another look and your confirmation on holding the button.

    The reason you’re still firing the DarkBullet spell despite you not being supposed to by code. I’ve found that your castOrhealTimer isn’t actually changing in getInput(). due to the line being slightly incorrect.

    Just add a + in castOrhealTimer = Time.deltaTime

        void getInput()
        {
            xAxis = Input.GetAxisRaw("Horizontal");
            yAxis = Input.GetAxisRaw("Vertical");
            attk = Input.GetButtonDown("Attack");
    
            if (Input.GetButton("Cast/Heal"))
            {
                castOrhealTimer += Time.deltaTime;
            }
            else
            {
                castOrhealTimer = 0;
            }
        }

    Then do tell whether it works now.

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

    Thank you for helping me. It work perfectly now. Very greatfully all of your help but I found another bug. If I’m dashing to the top of the enemy, my game is freezing and i cannot do anything. It looks like this

    how bug happend My character take dmg and then it freezing. It’s not the unity freeze because i still can stop the game or check the inspector. Here is the video how it happend. If this bug can fix in the up comming video so can you show me which part so i can fix it. Thanks you very much demo how the bug happend again sorry for take your time so much
    #15934
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    This bug is addressed in Part 6 of the series at the first minute mark.

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

    Hi so I know this is an old topic but I wanna ask that why my player casting the spell right after I realease the heal button. I remember that from the last time we fix it work ok but after a long time, I don’t really remember that did I change anything in the update method to make this bug. Would you see it for me?

    <code>private void Update()
    {
        if (playerStateList.incutscene) return;
        getInput();
        UpdateJump();
        RestoreTimeScale();
        if (playerStateList.dashing) return;
        FlashWhenInvi();
        Moving();
        Heal();
        if (playerStateList.healing) return;
        CastSpell();
        Flip();
        Jump();
        StartDash();
        Attack();
    }</code>

    Tks you very much

    #16175
    Chloe Lim
    Level 10
    Moderator
    Helpful?
    Up
    0
    ::

    It could be that the order of if (playerStateList.healing) return; and CastSpell(); is the other way round do swap the position for both

    #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 10
    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 10
    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 10
    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 12 posts - 16 through 27 (of 27 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: