Forum begins after the advertisement:


[Part 3] Recoiling while not hitting anything

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 3] Recoiling while not hitting anything

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #14144
    Liêm Trần
    Participant

    There is some problem with my project. The player keeps recolinging Y and attacking even when not hitiing any enemies.
    My attacking Code:

     void Attack() {
            timeSinceAttack += Time.deltaTime;
            if(Input.GetButtonDown("Attack") && attack && timeSinceAttack >= timeBetweenAttack) {
                timeSinceAttack = 0;
                anim.SetTrigger("Attacking");
    
                audioSource.PlayOneShot(attackSound);
    
                //* The player is not holding down or up
                if(yAxis == 0 || yAxis < 0 && Grounded()) {
    
                    int _recoilLeftOrRight = pState.lookingRight ? 1 : -1; 
                    Hit(sideAttackTransform, SideAttackArea, ref pState.recoilingX,Vector2.right * _recoilLeftOrRight, recoilXSpeed);
                    SlashEffectSide(slashEffect,0, sideAttackTransform);
                } else if(yAxis > 0) {
                    Hit(upAttackTransform, UpAttackArea, ref pState.recoilingY,Vector2.up, recoilYSpeed);
                    SlashEffectAtAngle(slashEffect, 90, upAttackTransform);
                } else if(yAxis < 0 && !Grounded()) {
                    Hit(downAttackTransform, DownAttackArea,ref pState.recoilingY,Vector2.down, recoilYSpeed);
                    SlashEffectAtAngle(slashEffect, -90, downAttackTransform);
    
                }
            }
        }
    

    Settings:

    Video demo
    please help my due project is coming the next two days

    #14168
    Joseph Tang
    Moderator

    Sorry for not seeing this earlier, however this doesn’t seem to be an issue with your Attack() method but your Hit() method, or Recoil() method. I suggest you drop the entire PlayerController.cs code for us to take a look at.

    What is likely happening is that everytime you attack, your recoilingX or recoilingY bool is being set to true, thus activating the Recoil() method that’s in Update() looking for either to be set to true. Thus, likely your Hit() method isn’t working correctly to prevent all attacks from setting the bool to true.

    For 1, check if your Hit() method set’s your recoil bool whenever it detects you have hit an object
    It would be this code:

        void Hit(Transform _attackTransform, Vector2 _attackArea, ref bool _recoilDir, float _recoilStrength)
        {
            Collider2D[] objectsToHit = Physics2D.OverlapBoxAll(_attackTransform.position, _attackArea, 0, attackableLayer);
            List hitEnemies = new List();
    
            if(objectsToHit.Length > 0)
            {
                _recoilDir = true;
            } 

    Ensure your code looks like this by the end of Part3

        void Hit(Transform _attackTransform, Vector2 _attackArea, ref bool _recoilDir, float _recoilStrength)
        {
            Collider2D[] objectsToHit = Physics2D.OverlapBoxAll(_attackTransform.position, _attackArea, 0, attackableLayer);
            List hitEnemies = new List();
    
             if(objectsToHit.Length > 0)
            {
                _recoilDir = true;
            } 
            for(int i = 0; i < objectsToHit.Length; i++)
            {
                Enemy e = objectsToHit[i].GetComponent();
                if(e && !hitEnemies.Contains(e))
                {
                    e.EnemyHit(damage, (transform.position - objectsToHit[i].transform.position).normalized, _recoilStrength);
                    hitEnemies.Add(e);
                }
            }
        }
        void SlashEffectAtAngle(GameObject _slashEffect, int _effectAngle, Transform _attackTransform)
        {
            _slashEffect = Instantiate(_slashEffect, _attackTransform);
            _slashEffect.transform.eulerAngles = new Vector3(0, 0, _effectAngle);
            _slashEffect.transform.localScale = new Vector2(transform.localScale.x, transform.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);
                }
                airJumpCounter = 0;
            }
            else
            {
                rb.gravityScale = gravity;
            }
    
            //stop recoil
            if(pState.recoilingX && stepsXRecoiled < recoilXSteps)
            {
                stepsXRecoiled++;
            }
            else
            {
                StopRecoilX();
            }
            if (pState.recoilingY && stepsYRecoiled < recoilYSteps)
            {
                stepsYRecoiled++;
            }
            else
            {
                StopRecoilY();
            }
    
            if(Grounded())
            {
                StopRecoilY();
            }
        }
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: