I need help stopping my player character from moving

So I chose to learn unity and C# as a project for my individual study class and the due date is Monday next week and I been having problems getting my player character to stop. So far, each solution I have tried resulted in no change or a error.

using UnityEngine;
using UnityEngine.InputSystem;

public class Player_Movement : MonoBehaviour
{
    // Using 2d as game is controled on X,Y

    //Public eaisy to edit
    public Rigidbody2D rb2d;

    public float yForce = 100f;

    public float xForce = 100f;

    public Transform groundCheck;

    public LayerMask groundLayer;

    float direction = 0;

 //private to avoid changing
    private bool isGrounded;

//Cheacks for controls and enables controler support all new controls aspects need to be added below here but above the Void Awake function.
    InputAction jumpAction;

    PlayerControls controls;

    private void Awake()
    {
        controls = new PlayerControls();
        controls.Enable();
//This was only needed for -/+ axis thank god
        controls.Land.Movement.performed += ctx => 
        {
            direction = ctx.ReadValue<float>();
        };
//Formating for all future Imput actions below "_Action = InputSystem.actions.FindAction("_");

        jumpAction = InputSystem.actions.FindAction("JumpingKeys");
        // Jumping Input action
    }

private void OnCollisionEnter2D(Collision2D collision)
{
    //Ground collision to check if jumping is possible, make sure to apply Tag=Ground to anything I want the player to jump off of.
    if (collision.gameObject.tag == "Ground")
    {
        isGrounded = true;
    }
}

    void FixedUpdate ()
    {
    //Basic movement
        //Need to figure out a way to make this reset Velocity to zero when nuter at a contolled pase or 
        // switch to AddForce2D which can lead to hole host of problums later. (problum below)
        rb2d.linearVelocity = new Vector2(direction * xForce, rb2d.linearVelocity.y);


            if (jumpAction.IsPressed())
            {
                if(isGrounded)
                    {
                        rb2d.AddForce(Vector2.up * yForce);
                    }
            }
        //Jumping
        
}
}

Any help will be appreciated. I was unable to get help earlier because posting on forums is banned on school WIFI.

I’ve pretty much always used AddForce. The only time I’ve manually set velocity to 0 (instead of letting the drag do it naturally) is when jumping if I don’t want any horizontal momentum to transfer into the jump.

I tied using AddForce but I could not get it working with the input system can you tell me how to do that? All the Input System tutorials I see dont use AddForce. If possible can you send a tutorial using AddForce and the input system?

Basically, wherever it changes the velocity manually, substitute AddForce and playtest, then tweak the force amount. No need for a specific tutorial, just find where they change it and use AddForce instead.

You can hook into InputAction.cancelled and set the velocity to zero in that callback.

Or you can instead poll the value per-frame rather than using the callbacks.

Be aware that setting the velocity of a rigidbody will not work well with applying forces (looking at your jump method).

1 Like

Thank you, you saved me a lot of time.

1 Like

I cant figure out the syntax for this can you help me. I tried using Addforce instead of velocity and it just did not work

How would I do that?

Do which of the two things I suggested?

Both I only been coding in c# for 4 weeks at this point.

You may find it easier to use the old input system until you gain more experience.

Can I set the velocity function to an if function so that I can use an else function to slow it down to do what you suggested?

Figured out problem with help from a friend in person
Ans is

   direction = controls.Movement.Move.ReadValue<float>();
        if(Math.Round(direction, 2) != 0){
            rb2d.linearVelocity = new Vector2(direction * xForce, rb2d.linearVelocity.y);
        } else{
            rb2d.linearVelocity = new Vector2(rb2d.linearVelocity.x * 0.95f, rb2d.linearVelocity.y);
        }

instead of Velocity function above and Preformed function solving for direction.