Search Unity

Can't seem to get clamp or rigidbody constraints working

Discussion in 'Scripting' started by EmperorFresh, Dec 19, 2014.

  1. EmperorFresh

    EmperorFresh

    Joined:
    Dec 17, 2014
    Posts:
    8
    Hey guys! I need a bit of help with this script for my sidescroller. I want to have the player stop moving when the space bar is held down, and, when released, have him resume movement unhindered. I've had some issues implementing this code, was hoping for some help.

    Code (CSharp):
    1.     void FixedUpdate ()
    2.     {
    3.         float moveHorizontal = Input.GetAxis ("Horizontal");
    4.         float moveVertical = Input.GetAxis ("Vertical");
    5.  
    6.         Vector3 movement = new Vector3 (moveHorizontal, moveVertical, 0.0f);
    7.         rigidbody.velocity = movement * speed;
    8.    
    9.         rigidbody.position = new Vector3
    10.             (
    11.                 Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
    12.                 Mathf.Clamp (rigidbody.position.y, boundary.yMin, boundary.yMax),
    13.                 0.0f
    14.             );
    15.  
    16.         if (Input.GetButtonDown("SpaceBar"))
    17.         {
    18.             rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY;
    19.         }
    20.  
    21.         if (Input.GetButtonUp("SpaceBar"))
    22.         {
    23.             rigidbody.constraints = RigidbodyConstraints.None;
    24.         }
    Here is the rigidbody.constraint attempt I tried first. It only works half the time, with the game occasionally not releasing the constraint. This results in the player pressing the space bar multiple times before the constraint will be lifted. The opposite is true also: sometimes the spacebar being pressed down sometimes doesn't work.

    Code (CSharp):
    1.     void FixedUpdate ()
    2.     {
    3.         float moveHorizontal = Input.GetAxis ("Horizontal");
    4.         float moveVertical = Input.GetAxis ("Vertical");
    5.  
    6.         Vector3 movement = new Vector3 (moveHorizontal, moveVertical, 0.0f);
    7.         rigidbody.velocity = movement * speed;
    8.    
    9.         rigidbody.position = new Vector3
    10.             (
    11.                 Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
    12.                 Mathf.Clamp (rigidbody.position.y, boundary.yMin, boundary.yMax),
    13.                 0.0f
    14.             );
    15.  
    16.         if (Input.GetButton("SpaceBar"))
    17.         {
    18.             rigidbody.position = new Vector3
    19.                 (
    20.                     Mathf.Clamp (rigidbody.position.x, 0.0f, 0.0f),
    21.                     Mathf.Clamp (rigidbody.position.y, 0.0f, 0.0f),
    22.                     Mathf.Clamp (rigidbody.position.z, 0.0f, 0.0f)
    23.                 );
    24.         }
    25.  
    Here is another attempt at it with clamps. This code hardly even works! It teleports the player into the middle of the game screen, and only marginally inhibits movement. By that I mean the player can still move only a little.

    If anybody could help me out that'd be awesome! Thanks for taking the time to read this thread. If you wish for the entire script you can download it here.
     

    Attached Files:

  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    First approach looks to be ok except for one slight hiccup. GetButtonDown and ...Up work based on the frame the user presses the button, you have these in FixedUpdate which doesn't necessarily execute every frame. Try having the user input checks in the Update function and have it set a bool for the fixed update to check against.

    Second approach teleports to the middle of the game screen because you are clamping to (0,0,0), you were actually wanting to Clamp the changes to the position to 0 not the position components themselves.
     
    EmperorFresh likes this.
  3. EmperorFresh

    EmperorFresh

    Joined:
    Dec 17, 2014
    Posts:
    8
    Thanks! It worked! You're a godsend, but I got one last, quick, question. I remember reading in the forums elsewhere that rigidbodies like being in FixedUpdate. Are you sure it's okay to keep it in Update?