Search Unity

  1. Unity 2018.3 is now released.
    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. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Problem clamping Rigidbody velocity

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

  1. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    53
    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:
    53
    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,545
    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
    xorpheous likes this.