Search Unity

how to add Mathf.Abs to if(rigidbody.velocity.magnitude < 0.5)

Discussion in 'Scripting' started by eyalpsr, Jul 13, 2021.

  1. eyalpsr

    eyalpsr

    Joined:
    Jun 20, 2021
    Posts:
    2
    i tried to add Mathf.Abs to it and it didnt work that what i tried

    if(rb.velocity.Mathf.Abs(magnitude) > 2 ){

    }
    it will really help me if you could say how to fix it
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    As someone already said on the other thread that you necroposted, There is no need and no point in using Mathf.Abs with the magnitude of a Vector3 because the magnitude of a vector is always positive. The magnitude is just the pythagorean theorem. So it calculates the square root of the sum of the squares of each component.

    So since the use of Abs is completely pointless it's not really clear what you want to achieve here. However, just for completeness, the correct syntax would be this:

    Code (CSharp):
    1. if(Mathf.Abs(rb.velocity.magnitude) > 2 ){
    2.  
    3. }
    Though as already said, it's completely pointless to do this. You get the exact same behaviour when you do

    Code (CSharp):
    1. if(rb.velocity.magnitude > 2 ){
    2.  
    3. }
    edit

    ps: When other suggested you should post your own question, they meant that you should go more into detail what you want to do. Now we're just at the same point that we have no idea what you actually want to achieve.
     
    Vryken and Kurt-Dekker like this.