Forum begins after the advertisement:


[Part 3/Part 4] Player not recoiling when hit

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 3/Part 4] Player not recoiling when hit

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #13104
    Luansian
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    I initially coded the recoil mechanics and they worked fine as far as I’m aware. I’m partway through part 4 and now, when the player hits the enemy everything recoils as normal, but when the enemy hits the player nothing happens.

    Player script:

        void Hit(Transform _attackTransform, Vector2 _attackArea, ref bool _recoilDir, float _recoilStrength)
        {
                Collider2D[] objectsToHit = Physics2D.OverlapBoxAll(_attackTransform.position, _attackArea, 0, attackableLayer);
    
                if(objectsToHit.Length > 0)
                {
                    _recoilDir = true;
                }
                for(int i = 0; i < objectsToHit.Length; i++)
                {
                    if (objectsToHit[i].GetComponent<Enemy>() != null)
                    {
                        objectsToHit[i].GetComponent<Enemy>().EnemyHit(damage,(transform.position - objectsToHit[i].transform.position).normalized, _recoilStrength);
                    }
                }
        }
    
        void SlashEffectAngle(GameObject _slashEffect, int _effectAngle, Transform _attackTransform)
        {
            _slashEffect = Instantiate(_slashEffect, _attackTransform);
            _slashEffect.transform.eulerAngles = new Vector3(0, 0, _effectAngle);
            _slashEffect.transform.localScale = new Vector2(_attackTransform.localScale.x, _attackTransform.localScale.y);
        }
    
        void Recoil()
        {
            if(pState.recoilingX)
            {
                if(pState.lookingRight)
                {
                    rb.velocity = new Vector2(-recoilXSpeed, 0);
                }
                else
                {
                    rb.velocity = new Vector2(recoilXSpeed, 0);
                }
            }
    
            if(pState.recoilingY)
            {
                rb.gravityScale = 0;
                if(yAxis < 0)
                {
                    rb.velocity = new Vector2(rb.velocity.x, recoilYSpeed);
                }
                else
                {
                    rb.velocity = new Vector2(rb.velocity.x, -recoilYSpeed);
                }
            }
            else
            {
                rb.gravityScale = gravity;
            }
            airJumpCounter = 0;
    
            // stop recoil
            if(pState.recoilingX && stepsXRecoiled < recoilXSteps)
            {
                stepsXRecoiled++;   
            }
            else
            {
                StopRecoilX();
            }
            if(pState.recoilingY && stepsYRecoiled < recoilYSteps)
            {
                stepsYRecoiled++;   
            }
            else
            {
                StopRecoilY();
            }
    
            if(Grounded())
            {
                StopRecoilY();
            }
        }
    
        void StopRecoilX()
        {
            stepsXRecoiled = 0;
            pState.recoilingX = false;
        }
        void StopRecoilY()
        {
            stepsYRecoiled = 0;
            pState.recoilingY = false;
        }

    Enemy script:

       [SerializeField] protected float health;
        [SerializeField] protected float recoilLength;
        [SerializeField] protected float recoilFactor;
        [SerializeField] protected bool isRecoiling = false;
    
        [SerializeField] protected PlayerController player;
        [SerializeField] protected float speed;
        [SerializeField] protected float damage;
        
    
        protected float recoilTimer;
        protected Rigidbody2D rb;
    
        protected virtual void Start()
        {
            
        }
    
        protected virtual void Awake()
        {
            rb = GetComponent<Rigidbody2D>();
            player = PlayerController.Instance;
        }
    
        protected virtual void Update()
        {
            if (health <= 0)
            {
                Destroy(gameObject);
            }
            if(isRecoiling)
            {
                if(recoilTimer < recoilLength)
                {
                    recoilTimer += Time.deltaTime;
                }
                else
                {
                    isRecoiling = false;
                    recoilTimer = 0;
                }
            }
        }
    
        public virtual void EnemyHit(float _damageDone, Vector2 _hitDirection,float _hitForce)
        {
            health -= _damageDone;
            if(!isRecoiling)
            {
                rb.AddForce(-_hitForce * recoilFactor * _hitDirection);
            }
        }
    
        protected void OnTriggerStay2D(Collider2D _other)
        {
            if(_other.CompareTag("Player") && !PlayerController.Instance.pState.invincible)
            {
                Attack();
                PlayerController.Instance.HitStopTime(0, 5, 0.5f);
            }
        }
    
        protected virtual void Attack()
        {
            PlayerController.Instance.TakeDamage(damage);
        }
    }
    

    I don’t know if somewhere along the way I forgot to give the enemy a hit force, but the recoiling state is also not triggering on the player either.

    thank you! :))

    btw absolutely adore the series so far! everything is very well-explained and this is the first issue I came across that couldn’t be fixed by rewatching carefully/looking at the blog. very grateful for the blog posts and forum <3

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

    Thanks Luansian,

    Glad you like the series. Usually, if the recoil is not registering, it’s because either the player or enemy prefab is not tagged correctly. Can you check if this is so?

    Also, I’m aware that there is some information in the video that isn’t in the blog. If you come across any, I’ll appreciate if you can let me know about it so I can update the article.

    #13117
    Luansian
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    Ty for the fast reply! Turns out it was a misunderstanding on my part, I didn’t realise that the enemy’s hit wasn’t meant to make the player recoil.

    I didn’t notice anything missing in the video, sorry.

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: