Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Problem clamping Rigidbody velocity

Discussion in 'Physics' started by crdmrn, Nov 7, 2018.

  1. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    41
    Hello,
    I'm trying to clamp the velocity of a Rigidbody, as you can see in the code below.
    As far as I can tell, the process I'm using should be correct, also because the debug log outputs the right value.
    Visually tho, when I change the acceleration value, the Rigidbody not only accelerates faster (as it should happen) but it also seems to move faster (through the debug.log still tells me that the velocity vector is clamped)...
    Any Idea why?

    Code (CSharp):
    1. private void FixedUpdate() {
    2.  
    3.     Vector3 direction = new Vector3(input.x, 0f, input.y);
    4.  
    5.     playerRigidbody.AddForce(direction * acceleration, ForceMode.VelocityChange);
    6.  
    7.     if (playerRigidbody.velocity.magnitude > maxSpeed)
    8.         playerRigidbody.velocity = Vector3.ClampMagnitude(playerRigidbody.velocity, maxSpeed);
    9.  
    10.     Debug.Log(playerRigidbody.velocity.magnitude);
    11. }
     
    Last edited: Nov 10, 2018
  2. xorpheous

    xorpheous

    Joined:
    Mar 28, 2018
    Posts:
    10
    I think you're missing something in your conditional statement.
    Code (CSharp):
    1. if (playerRigidbody.magnitude > maxSpeed)
    I think it should be
    Code (CSharp):
    1. if (playerRigidbody.velocity.magnitude > maxSpeed)
     
    SparrowsNest likes this.
  3. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    41
    It was a typo, I cpoied my code and removed the comments to have it take less space in my post and somehow deleted that part as well.
    Now the code is exactly as I have it, and the outcome is still as described :S
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,504
    Adding a force in 3D physics doesn't actually happen until the simulation step. The velocity before and after you're adding force will be the same. You're better off directly manipulating the velocity here like so:

    Code (CSharp):
    1.     private void FixedUpdate()
    2.     {
    3.         var force = new Vector3(input.x, 0f, input.y) * acceleration;
    4.         var velocity = playerRigidbody.velocity + force;
    5.         playerRigidbody.velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
    6.     }
    7.  
     
    Last edited: Nov 11, 2018 at 6:59 PM
    xorpheous likes this.