Search Unity

rigidBody.velocity giving odd readings

Discussion in 'Physics' started by Deleted User, Jul 1, 2019.

  1. Deleted User

    Deleted User

    Guest

    Hello. Following Project Boost lessons on Udemy has got me to make a game where a missile has to be going at its top speed to blow up a bomb at the end of the level.
    I thought using Vector3 and magnitude or velocity would give me a reading, that the faster my rocket goes, the higher the number it returns.

    After giving it a shot, firstly I found negative numbers being shown. Then after using Math.Abs I noticed that I was getting readings that varied from 0.1 to 0.3 when acting out the same action. So I was getting different results.
    The only way I knew was to average out the X and Y velocities by dividing by two. But still when the rocket goes fast I get a result that can vary from 0.6 to 10.0.

    Code (CSharp):
    1.         float X = this.rigidBody.velocity.x;
    2.         float Y = this.rigidBody.velocity.y;
    3.         float absX = Math.Abs(X);
    4.         float absY = Math.Abs(Y);
    5.         float mag = (absX + absY) / 2f;
    Anybody with a solution to finding out if the rocket is going fast, or slower than the bomb at the end needs?
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    You want the magnitude of the velocity vector.
    rigidBody.velocity.magnitude
    This removes the direction of velocity in multiple dimensions and only considers the single number of speed. Your mag calculation is not correct; the magnitude is the
    sqrt(x*x+y*y+z*z)
    .
     
  3. Deleted User

    Deleted User

    Guest

    Thanks halley… awesome picture ;)

    It doesn't seem that my results are what I expect still. I fly the rocket around my map and crash into walls. When the rocket crashes Debug.Log shows me its magnitude.

    When testing this, if I spun my rocket while crashing it added extra force and upped the magnitude. But after repeating the same crash (up, across, spin around and hit the floor).. the magnitude varied from 0.4 to 3.0.

    This doesn't give me consistency. If I wanted the rocket to crash into a bomb at the end of the level, one crash would not register above (say 3.0 magnitude) what I've set, yet one would. It wouldn't be very fair to the player.

    Code (CSharp):
    1.     private void KillPlayer()
    2.     {
    3.         float mag = this.rigidBody.velocity.magnitude;
    4.         Debug.Log(mag);
    5.  
    6.         state = State.Dying;
    7.         audioSource.Stop();
    8.         mainEngineParticles.Stop();
    9.         audioSource.PlayOneShot(deathSound);
    10.         deathSoundParticles.Play();
    11.         Invoke("LoadFirstLevel", 2f);
    12.     }
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    We're gonna need to see more about how you're moving this object around, then.
    Debug.Log
    will often round numbers to very few digits excessively, but shouldn't confuse 0.3 with 4.0.
     
  5. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1.     void RespondToThrustInput()
    2.     {
    3.         if (Input.GetKey(KeyCode.Space))
    4.         {
    5.             rigidBody.AddRelativeForce(Vector3.up * thrustSpeed * Time.deltaTime);
    6.         }
    7.  
    8.     private void RespondToRotateInput()
    9.     {
    10.         float rotationThisFrame = turningSpeed * Time.deltaTime;
    11.  
    12.         if (Input.GetKey(KeyCode.A))
    13.         {
    14.             transform.Rotate(Vector3.forward * rotationThisFrame);
    15.         }
    16.         else if (Input.GetKey(KeyCode.D))
    17.         {
    18.             transform.Rotate(-Vector3.forward * rotationThisFrame);
    19.         }
    20.     }
     
  6. Deleted User

    Deleted User

    Guest

    Don't sweat it too much. I think the forums can be seen as an answer base, when really you need to spend more time in Unity. It's like I want the answer now. But I'll stumble across a solution in a year's time :)