Stop Player Movement - Unity C#

 How to make Player stop to move 



To stop moving a Character like a Player in Unity with c# script you have to follow the bellow steps :

  1. determine when the character( player) can move.
  2. determine when the the character stop to move
  3. if you Have the animation you must add it when the the character stop the movement .

1. to determine when the player can move :


  • add the code :


    public bool canMove;


  • in Start() method add the following line:



 void Start()
    {
        ..
        ..
        canMove = true; //add this line
    }

  • In the Update() method add this code ,an inside them ad your lines that are you using for activate the movement :
this is a example for endless runner movement :

   

private Vector3 move;
    public float forwardSpeed;
----

----

    public bool canMove; // add this line


    void Update()
    {

-------
---------


        if(canMove) //Add this line
        {
            animator.SetBool("isGameStarted", true);
        move.z = forwardSpeed;

        ---
----

        }
    }


2 . determine when the the character stop to move :

  • in the function when your player or character mut stopping the movement you need to add this line :





            canMove = false;
           
            Debug.Log("win");


            move.z = 0.0f;
 

3 . if you Have the animation you must add it when the the character stop the movement .

  • Example for Animation :

animator.SetBool("win", true);


if you need additional explain let me know in a comment

...

C#,stop object movement unity,unity freeze position,unity stop movement on collision,freeze player unity,unity lock player movement,unity 2d stop movement in collision,
unity player movement 2d,unity movement script,unity character controller jump,simple movement unity,player controller unity,unity make player move,unity move with keyboard,fps camera script unity