Why RigidBody2D gains supernatural force when encountering an Edge Collider? (674513)

Hello,

I have a RigidBody2D and while I’m moving it horizontally it gains a supernatural strength when it encounters some pointed collider … why does this happen? I have tried to apply some settings in RigidBody2D but I did not succeed.

What is wrong?

!(http://

Please see the video:

Every help is welcome!)

Would you mind posting the code that drives your character’s movement? It would help a lot to figure out the source of the issue. Please use code tags.

Hello @LiterallyJeff , see this please:

private void Update()
    {

        //Time.timeScale = 0.7f;

        GroundDetection();

        command = EnumPlayerCommands.Move;

        if (Input.GetKeyDown(KeyCode.Space) && _grounded)
            command = EnumPlayerCommands.Jump;

        if (HORIZONTAL_AXIS > 0 && !_facingRight)
            command = EnumPlayerCommands.Flip;
        else if (HORIZONTAL_AXIS < 0 && _facingRight)
            command = EnumPlayerCommands.Flip;

    }

    void FixedUpdate()
    {
        switch (command)
        {
            case EnumPlayerCommands.Move:
                MoveHorizontal();
                break;
            case EnumPlayerCommands.Jump:
                Jump();
                break;
            case EnumPlayerCommands.Flip:
                Flip();
                break;
            default:
                break;
        }

        HORIZONTAL_AXIS = Input.GetAxisRaw("Horizontal"); //-1,0,1

        animator.SetFloat("vertical_speed", rgb2d.velocity.y);
    }

    void MoveHorizontal() {
        var hVelocity = moveSpeedHorizontal * HORIZONTAL_AXIS;
        rgb2d.velocity = new Vector2(hVelocity, rgb2d.velocity.y);
        animator.SetFloat("horizontal_speed", Mathf.Abs(hVelocity));
    }

    void Jump() {
        rgb2d.AddForce(new Vector2(0, jumpForce));
    }

Well, the solution doesn’t immediately jump out to me, but some things to try:

Move your horizontal input polling to Update, you want the inputs to be frame-accurate. You may also want to try using “rgb2d.AddForce(Vector2.right * hVelocity)” instead of setting velocity directly. You’re probably overwriting any deceleration due to resistance or friction caused by the surface at a steep angle, possibly causing the upward motion as it’s redirected each frame by the physics system correcting for collisions.

Hey…

I tried what you suggested, it seems to me that something has improved. But the momentum did not stop when I kept pressing against the collider.

    void MoveHorizontal() {
        var hVelocity = moveSpeedHorizontal * HORIZONTAL_AXIS;
        //rgb2d.velocity = new Vector2(hVelocity, rgb2d.velocity.y);
        rgb2d.AddForce(Vector2.right * hVelocity);
        animator.SetFloat("horizontal_speed", Mathf.Abs(hVelocity));
    }

    private void Update()
    {


        HORIZONTAL_AXIS = Input.GetAxisRaw("Horizontal"); //-1,0,1
      ...
    }

Yeah, I would expect it to continue pushing, but at least now it will take into account deceleration. Is it possible that given your gravity and speed settings, it’s simply using that angle as a ramp? If your goal is to keep the player on the ground at all times while running, you could do a raycast downwards and move the player to the ground, then stop raycasting during a jump until grounded again.

Exactly … at that point we not want the player to use it as a ramp.

So I would have to do a function that keeps pulling him down while he walks?

The physics system is designed to be as realistic as possible, so if you want something unrealistic like an object not carrying momentum off an incline, you’ll need to code something to make that behavior happen.

You’re absolutely right. I’ll think of something that solves this problem. Thank you very much for the clarification.

One last help … you think this problem: (https://forum.unity3d.com/threads/camera-smooth-player-moves-little-bit-back-when-jump.489271/) Does it also have to do with physics?