- Home /
How to stop player from moving
I have a 2d platformer game, and I want to stop the player from moving when a specific event happens. In this case, the event is a bool called 'onGame'. Here is my script, or at least what you need to see of it.
 private void FixedUpdate()
     {
         if (!OpenGame.gameOn)
         {
             
             float moveInput = Input.GetAxisRaw("Horizontal");
             rb.velocity = new Vector2(moveInput * movementSpeed, rb.velocity.y);
 
             if (moveInput == 0)
             {
                 anim.SetBool("isRunning", false);
             }
             else
             {
                 anim.SetBool("isRunning", true);
             }
 
             if (moveInput < 0 && !facingRight)
             {
                 flip();
             }
             else if (moveInput > 0 && facingRight)
             {
                 flip();
             }
 
            
         }
         if (OpenGame.gameOn)
         {
             movementSpeed = 0f;
         }
     }
now for some reason, when gameOn becomes active, if the player was still holding down one of the movement keys, than the player would still continue moving in that direction. I even tried setting the movement speed to 0 to stop the player from moving, but it just doesnt work. Any help is greatly appreciated.
Answer by Stiexeno · Feb 18 at 08:18 PM
Simple solution to enable Kinematic to prevent Rigidbody velocity. Don't forget to disable Kinematic to continue movement
         public Rigidbody playerRb;
 
         public void StopPlayer()
         {
             playerRb.isKinematic = true;
         }
wow, that's a smart solution. I guess I just had to think outside the box, but thanks!
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                