Unwanted player movement created by player action

I’m having a new issue today that I can’t seem to explain. Issue is that my 2D player character is moving ever so slightly every time I fire an arrow. Here’s a video for reference:

https://imgur.com/U7kjv1C

This is a new bug, but I’ve done so much I’m not sure where to look back to. I haven’t adjusted the player code or animation surrounding the bow and arrow, so I’m feeling pretty confused by it.

Here’s the current player script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Rewired;




public class BitchFaceController : MonoBehaviour
{

    public int playerId = 0;
    private Player player;
    public bool useController;
    public Rigidbody2D rb;
    public GameObject arrowPrefab;

    //Animators
    public Animator topAnimator;
    public Animator bottomAnimator;
    public GameObject crossHair;

    //Movement
    Vector2 movement;
    public float speed;
    private float lastDirX;
    private float lastDirY;
    private float currDirX;
    private float currDirY;

    //Bow
    Vector2 aim;
    bool isAiming;
    bool endAiming;
    public float arrowSpeed;

    //Sword
    Vector2 crossHairDif;
    Vector2 crossHairDir;
    bool isSlicing;
    bool endSlicing;
    private float crossHairDirX;
    private float crossHairDirY;
    private float crossHairDirXAnim;
    private float crossHairDirYAnim;

    //LERP Dodge Roll
    public bool isDodging;
    private float fracJourney;
    public Vector2 startMarker;
    public Vector2 endMarker;
    public float dodgeSpeed;
    public float dodgeDistance;
    public float animationSpeed;
  




    private void Awake()
    {
        Debug.Log("PlayerId is " + playerId.ToString());
        player = ReInput.players.GetPlayer(playerId);

        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    private void Start()
    {
        rb = this.GetComponent<Rigidbody2D>();
        arrowSpeed = 3;
    }

    // Update is called once per frame
    void Update()
    {
        ProcessInputs();
        AimAndShoot();
        Animate();
    }

    private void FixedUpdate()
    {
        Move();
        Dodge();
    }
  

    //Moving
    private void Move()
    {
        rb.MovePosition(rb.position + (movement * speed * Time.fixedDeltaTime));
        rb.velocity = new Vector2(movement.x, movement.y);
    }


    //Set direction parameters for sword slicing animation blend tree coordinates
    void SwordAnimation()
    {
        crossHairDif = transform.position - crossHair.transform.position;

        if (crossHairDif.magnitude < 1)
        {
            crossHairDir = crossHairDif.normalized;
        }
      
        crossHairDirX = -crossHairDir.x;
        crossHairDirY = -crossHairDir.y;
    }


    //Dodging
    private void Dodge()
    {
        if (!isDodging && Input.GetKey(KeyCode.Space) && (rb.velocity.x != 0 || rb.velocity.y != 0))
        {
            isDodging = true;
            fracJourney = 0;
   
            //startMarker = transform.position;
            startMarker = rb.position;
            endMarker = rb.position + movement * dodgeDistance;
            //endMarker = rb.MovePosition(rb.position + (movement * dodgeDistance));

            topAnimator.SetBool("Dodge", true);
        }

        if (isDodging)
        {
            fracJourney = fracJourney + Time.deltaTime * dodgeSpeed;
          

            if (fracJourney > 1)
            {
                fracJourney = 1;
                isDodging = false;

                topAnimator.SetBool("Dodge", false);
                //this.Archer_Bottom.enabled = true;
            }

            rb.MovePosition(Vector2.Lerp(startMarker, endMarker, fracJourney));
        }  
    }

    //Processing Inputs, Aiming, and Walk Animation
    private void ProcessInputs()
    {      
            movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
            Vector2 mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
            aim = aim + mouseMovement;
            if (aim.magnitude > 1.0f)
            {
                aim.Normalize();
            }
            isAiming = Input.GetButton("Fire2");
            endAiming = Input.GetButtonUp("Fire2");
            isSlicing = Input.GetButtonDown("Fire1");

        if (movement.magnitude > 1.0f)
        {
            movement.Normalize();
        }

        //Set direction parameters for moving and idling
        if (Input.GetKey(KeyCode.W))
        {
            currDirX = 0;
            currDirY = 1;
            lastDirX = 0;
            lastDirY = 1;
        }
        else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
        {
            currDirX = -1;
            currDirY = 1;
        }
        else if (Input.GetKey(KeyCode.A))
        {
            currDirX = -1;
            currDirY = 0;
            lastDirX = -1;
            lastDirY = 0;
        }
        else if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.S))
        {
            currDirX = -1;
            currDirY = -1;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            currDirX = 0;
            currDirY = -1;
            lastDirX = 0;
            lastDirY = -1;
        }
        else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
        {
            currDirX = 1;
            currDirY = -1;
        }
        else if (Input.GetKey(KeyCode.D))
        {
            currDirX = 1;
            currDirY = 0;
            lastDirX = 1;
            lastDirY = 0;
        }
        else if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.W))
        {
            currDirX = 1;
            currDirY = 1;
        };

        SwordAnimation();

        //Set direction parameters for sword animation
        if (crossHairDirX < 0)
        {
            crossHairDirXAnim = -1;
        }

        if (crossHairDirX > 0)
        {
            crossHairDirXAnim = 1;
        }

        if (crossHairDirY <= 0.5f && crossHairDirY >= -0.5f)
        {
            crossHairDirYAnim = 0;
        }

        if (crossHairDirY < -0.5f)
        {
            crossHairDirYAnim = -1;
        }

        if (crossHairDirY > 0.5f)
        {
            crossHairDirYAnim = 1;
        }


    }




    private void Animate()
    {
        //Moving Legs
        bottomAnimator.SetFloat("MoveHorizontal", movement.x);
        bottomAnimator.SetFloat("MoveVertical", movement.y);
        bottomAnimator.SetFloat("MoveMagnitude", movement.magnitude);

        //Moving Body
        topAnimator.SetFloat("MoveHorizontal", currDirX);
        topAnimator.SetFloat("MoveVertical", currDirY);
        topAnimator.SetFloat("MoveMagnitude", movement.magnitude);

        //Shooting
        topAnimator.SetFloat("AimHorizontal", aim.x);
        topAnimator.SetFloat("AimVertical", aim.y);
        topAnimator.SetFloat("AimMagnitude", aim.magnitude);
        topAnimator.SetBool("Aim", isAiming);

        //Slicing
        topAnimator.SetFloat("CrossHairDirX", crossHairDirXAnim);
        topAnimator.SetFloat("CrossHairDirY", crossHairDirYAnim);
        topAnimator.SetBool("SwordSlice", isSlicing);

        //Idle Direction
        topAnimator.SetFloat("LastMoveX", lastDirX);
        topAnimator.SetFloat("LastMoveY", lastDirY);
    }

    private void AimAndShoot()
    {
        Vector2 shootingDirection = new Vector2(aim.x, aim.y);

        if (aim.magnitude > 0.0f)
        {
            crossHair.transform.localPosition = aim * 0.4f;
            crossHair.SetActive(true);

            shootingDirection.Normalize();
            if (endAiming)
            {
                GameObject arrow = Instantiate(arrowPrefab, transform.position, Quaternion.identity);
                Arrow1 arrow1Script = arrow.GetComponent<Arrow1>();
                arrow1Script.velocity = shootingDirection * arrowSpeed;
                arrow1Script.player = gameObject;
                arrow.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
                Destroy(arrow, 1.0f);
            }
        }
        else
        {
            crossHair.SetActive(false);
        }
    }



}

I didn’t look at the code but it could be that your player’s collider is intersecting with the arrow’s collider, so they are both being pushed apart when you fire it.

Go to your physics settings and disable their layers from interacting with each other and see if it works.

Anyway, when things like this happen and you can’t immediately know why I recommend you make a copy of the project and strip away everything that is not needed to reproduce this behavior and go from there. So both your project and code will have the minimal stuff necessary to make this happen while at the same time it will be easier to share it on the internet for help.

Ok I’ll try that out. And what do you mean by the second comment? Suggesting posting the project package online?

No, I mean that if you can reproduce the bug with the minimum steps possible you can easily find a way to fix it. For example, you don’t even need sprites, you can just look at their colliders and etc. But in case you still don’t find a way to solve it, at least now you have a small script/project to show to someone. And instead of being greeted with a script with 300 lines it will be much less like 10 or so.

Oh oh, I know what you’re saying. Typically I do (I’m on here a lot). I’ve just exhausted myself on this issue and haven’t been able to resolve it. I try not to post large code.

Ok so turned out to be the issue. When I disable the collider on the arrow I have no more issue. HOWEVER, when I adjust the settings in the project settings it doesn’t seem to do anything! What else could I be doing wrong here? My understanding is that this, essentially, is the same thing as disabling the collider for that specific layer, yes?

Posting arrow code, if this helps at all:

    public Vector2 velocity = new Vector2(0.0f, 0.0f);
    public GameObject player;

    public int damageToDeal;
    public GameObject damageBurst;
    public GameObject damageNumber;


    // Update is called once per frame
    void Update()
    {
        Vector2 currentPosition = new Vector2(transform.position.x, transform.position.y);
        Vector2 newPosition = currentPosition + velocity * Time.deltaTime;

        Debug.DrawLine(currentPosition, newPosition, Color.red);

        RaycastHit2D[] hits = Physics2D.LinecastAll(currentPosition, newPosition);

        foreach (RaycastHit2D hit in hits)
        {
            GameObject other = hit.collider.gameObject;
            if (other != player)
            {

                if (other.CompareTag("Structure"))
                {
                    Destroy(gameObject);
                    Debug.Log(other.name);
                    break;
                }

                if (other.CompareTag("Enemy"))
                {
                    Destroy(gameObject);
                    other.gameObject.GetComponent<EnemyHealthManager>().HurtEnemy(damageToDeal);
                    Instantiate(damageBurst, other.transform.position, other.transform.rotation);
                    var clone = (GameObject)Instantiate(damageNumber, other.transform.position, Quaternion.Euler(Vector3.zero));
                    clone.GetComponent<FloatingNumbers>().damageNumber = damageToDeal;
                    Debug.Log(other.name);
                    break;
                }
            }
           
        }

        transform.position = newPosition;
    }

What do you mean by this? If by disabling the collider your player doesn’t get pushed it means they are still interacting in the physics layers when you re-enable it.

Have you tried giving the player and the arrow prefab a different physics layer? Screenshot by Lightshot

Then click the boxes to disable both layers from interacting with each other in Player Settings → Physics/Physics2D: Screenshot by Lightshot

Is this how you did it? If not, try this and report back.

Oh man I feel dumb, but I didn’t realize there was a difference between physics and physics 2D.

Updated the Physics 2D settings as well and that absolutely did it.

I can’t thank you enough for your help!

No problem! I’m glad to have helped you. Good luck with your project.

1 Like