2DPhysics affect each other

Hello there, I wanted to ask for a helping hand for my 2D code. It seems like the velocity of my player movement influences the velocity/AddForce method of my dash, because when I dash at the position (0,0) I end up by either the positions (6.0f,0), (6.4f,0) or (6.8f,0). Even though when I debug it’s always the same velocity. Does somebody know a better way to code this or change it so there is a constistent movement?
I use a material on my 2D box collider with no friction, but it seems like it doesn’t cause this problem.
If you need more stuff to know, let me know.

public class PlayerInput : PlayerSystem
{
    float horizontalVelocity;
    string horizontalAxis => "Horizontal";
    string jumpButton => "Jump";
    KeyCode slash => KeyCode.E;
    KeyCode dash => KeyCode.Q;
    bool isDashing = false;
    float setTimer = 0.3f;
    float timer;
    SpriteRenderer spriteRenderer;

    protected override void Awake()
    {
        base.Awake();
        spriteRenderer = transform.GetComponent<SpriteRenderer>();
    }

    private void Update()
    {
        if (!PauseManager.gameIsPaused)
        {

            //Input dash
            if (Input.GetKeyDown(dash))
            {
                isDashing = true;
                player.ID.events.OnDashInput?.Invoke();
                timer = setTimer;

            }

            //Input move
            if (!isDashing)
            {
                horizontalVelocity = Input.GetAxisRaw(horizontalAxis);

                Flipping(horizontalVelocity);
                player.ID.events.OnMoveInput?.Invoke(horizontalVelocity);

                if (Input.GetButtonDown(jumpButton))
                {
                    player.ID.events.OnJumpInput?.Invoke();
                }

                //Input slash
                if (Input.GetKeyDown(slash))
                {
                    player.ID.events.OnSlashInput?.Invoke(horizontalVelocity);
                }
            }

            if (timer <= 0f)
            {
                isDashing = false;
            }

            if (timer > 0f)
            {
                timer -= Time.deltaTime;
            }
        }
    }

    void Flipping(float direction)
    {
        if (direction < 0f)
        {
            spriteRenderer.flipX = true;
        }
        else if (direction > 0f)
        {
            spriteRenderer.flipX = false;
        }
    }
}

public class PlayerMovement : PlayerSystem
{
    Rigidbody2D rb;

    [Header("Movement Settings")]
    [SerializeField] float speed = 15f;
    [SerializeField] float jumpForce = 30f;
    [SerializeField] float jumpTime = 1.5f;
    [SerializeField] float coyoteTime = 0.07f;
    float jumpTimeCounter;
    float coyoteTimeCounter;
    [SerializeField] float lowJumpMultiplier = 30f;
    [SerializeField] float fallMultiplier = 9f;
    int jumpCount;

    public Collider2D isGrounded;
    [SerializeField] Transform feetTrans;
    [SerializeField] float checkRadius = 0.7f;
    [SerializeField] LayerMask whatIsGround;

    protected override void Awake()
    {
        base.Awake();
        rb = transform.GetComponent<Rigidbody2D>();
    }

    void FixedUpdate()
    {
        isGrounded = Physics2D.OverlapBox(feetTrans.position, new Vector3(checkRadius, checkRadius, 0f), 0f, whatIsGround);
    }

    private void Update()
    {
        //Looking for Ground
        if (isGrounded)
        {
            coyoteTimeCounter = coyoteTime;
            jumpCount = 0;
            jumpTimeCounter = 0f;
        }
        else
        {
            coyoteTimeCounter -= Time.deltaTime;
        }

        if (jumpTimeCounter > 0f)
        {
            jumpTimeCounter -= Time.deltaTime;
        }

        ApplyGravity();
    }

    void HorizontalVelocity(float move)
    {
        rb.velocity = new Vector2(move * speed, rb.velocity.y);
    }

    void StopHorizontalVelocity()
    {
        rb.velocity = new Vector2(0f, rb.velocity.y);
    }

    void HandleJump()
    {
        jumpTimeCounter = jumpTime;

        //Double Jump integrate later, but it works!
        if (jumpTimeCounter > 0f && (isGrounded || coyoteTimeCounter > 0f || jumpCount < 1))
        {
            Jump();
        }
    }

    void Jump()
    {
        rb.velocity = new Vector2(rb.velocity.x, 0f);
        rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
        jumpCount++;
        jumpTimeCounter = 0f;
    }

    void ApplyGravity()
    {
        if (rb.velocity.y < 0f)
        {
            rb.gravityScale = fallMultiplier;
        }
        else if (rb.velocity.y > 0f && !Input.GetButton("Jump"))
        {
            rb.gravityScale = lowJumpMultiplier;
        }
        else
        {
            rb.gravityScale = 7f;
        }
    }

    void OnEnable()
    {
        player.ID.events.OnMoveInput += HorizontalVelocity;
        player.ID.events.OnNoMoveInput += StopHorizontalVelocity;
        player.ID.events.OnJumpInput += HandleJump;
    }

    private void OnDisable()
    {
        player.ID.events.OnMoveInput -= HorizontalVelocity;
        player.ID.events.OnNoMoveInput -= StopHorizontalVelocity;
        player.ID.events.OnJumpInput -= HandleJump;
    }
}

public class Dash : PlayerSystem
{
    [SerializeField] float acceleration = 4.322f;
    [SerializeField] float dashingTime = 0.3f;
    float dashTimeCooldown;
    public bool isAlreadyDashed;
    int isFacingLeft;
    Rigidbody2D rb;
    SpriteRenderer spriteRenderer;
    PlayerMovement playerMovement;

    protected override void Awake()
    {
        base.Awake();
        rb = transform.GetComponent<Rigidbody2D>();
        spriteRenderer = transform.GetComponent<SpriteRenderer>();
        playerMovement = transform.GetComponent<PlayerMovement>();
    }

    private void FixedUpdate()
    {
        if (playerMovement.isGrounded)
        {
            isAlreadyDashed = false;
        }

        if (dashTimeCooldown > 0f)
        {
            dashTimeCooldown -= Time.deltaTime;
        }
    }

    void Dashing()
    {

        if (!isAlreadyDashed)
        {
            isFacingLeft = spriteRenderer.flipX ? -1 : 1;
            dashTimeCooldown = dashingTime;
        }

        if (dashTimeCooldown > 0f)
        {
            isAlreadyDashed = true;
            rb.AddForce(isFacingLeft * Mathf.Pow(2, acceleration) * Vector2.right, ForceMode2D.Impulse);
        }

    }

    private void OnEnable()
    {
        player.ID.events.OnDashInput += Dashing;
    }

    private void OnDisable()
    {
        player.ID.events.OnDashInput -= Dashing;
    }
}

Hi, Vampir_Yoshi. One of our developers took a look at your code and responded with this:

It seems that the difference comes from the fact that the initial velocity that is given by the dash is only stopped by the HorizontalVelocity method, which is executed in Update. Depending on the framerate, there is a chance that several FixedUpdate will run that last frame before it gets stopped.

Hope this helps you out.

1 Like