Search Unity

Y velocity going crazy when landing on a surface.

Discussion in 'Physics' started by Callz01, Jul 21, 2019.

  1. Callz01

    Callz01

    Joined:
    Apr 9, 2019
    Posts:
    16
    Hey there, so I have this rocket ship (It's just a triangle right now). When it lands on a surface It's Y velocity becomes something like "7.806205e-09" and never stops even though I'm not moving. I really need help sorting this out because I'm checking if the velocity magnitude is less than "0.9f" and if it is then I debug.log something "Landed" however that never happens because of this issue.

    My ship code:
    Code (CSharp):
    1. public class ModuleController : MonoBehaviour
    2. {
    3.     Rigidbody2D rb;
    4.  
    5.     [SerializeField]
    6.     private float _upwardsForce;
    7.     [SerializeField]
    8.     private float _fuel;
    9.     [SerializeField]
    10.     private float _currentVelocity;
    11.  
    12.     [SerializeField]
    13.     private Text _fuelText;
    14.     [SerializeField]
    15.     private Text _velocityText;
    16.  
    17.     private void Start()
    18.     {
    19.         rb = GetComponent<Rigidbody2D>();
    20.     }
    21.  
    22.     private void Update()
    23.     {
    24.         _fuelText.text = "Fuel: " + _fuel.ToString("0.0");
    25.         _velocityText.text = "Velocity: " + rb.velocity.ToString("0.000");
    26.  
    27.         _currentVelocity = rb.velocity.magnitude;
    28.  
    29.         //RotateModule();
    30.     }
    31.  
    32.     private void FixedUpdate()
    33.     {
    34.         UpwardsForce();
    35.     }
    36.  
    37.     private void UpwardsForce()
    38.     {
    39.         if (Input.GetKey(KeyCode.W))
    40.         {
    41.             if (_currentVelocity > 3.5f)
    42.                 return;
    43.  
    44.             if (_fuel > 0)
    45.             {
    46.                 rb.AddForce(transform.up * _upwardsForce, ForceMode2D.Force);
    47.                 _fuel -= 0.5f;
    48.             }
    49.         }
    50.         else if (Input.GetKey(KeyCode.A))
    51.         {
    52.             if (_currentVelocity > 2.5f)
    53.                 return;
    54.  
    55.             if (_fuel > 0)
    56.             {
    57.                 rb.AddForce(-transform.right * _upwardsForce, ForceMode2D.Force);
    58.                 _fuel -= 0.2f;
    59.             }
    60.         }
    61.         else if (Input.GetKey(KeyCode.D))
    62.         {
    63.             if (_currentVelocity > 2.5f)
    64.                 return;
    65.  
    66.             if (_fuel > 0)
    67.             {
    68.                 rb.AddForce(transform.right * _upwardsForce, ForceMode2D.Force);
    69.                 _fuel -= 0.2f;
    70.             }
    71.         }
    72.     }
    73.  
    74.     private void RotateModule()
    75.     {
    76.         if (Input.GetKey(KeyCode.Q))
    77.         {
    78.             transform.rotation *= Quaternion.Euler(0f, 0f, 1f);
    79.             _fuel -= 0.1f;
    80.         }
    81.         else if (Input.GetKey(KeyCode.E))
    82.         {
    83.             transform.rotation *= Quaternion.Euler(0f, 0f, -1f);
    84.             _fuel -= 0.1f;
    85.         }
    86.     }
    87.  
    88.     private void OnCollisionEnter2D(Collision2D collision)
    89.     {
    90.         if (collision.gameObject.CompareTag("Surface"))
    91.         {
    92.             if (rb.velocity.magnitude < -1.4f)
    93.             {
    94.                 Debug.Log("Safely Landed!");
    95.             }
    96.             else if (rb.velocity.magnitude > -1.6f)
    97.             {
    98.                 Debug.Log("Crashed!");
    99.             }
    100.         }
    101.     }
    102. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    "7.806205e-09" is scientific notation (E-notation) for the crazy tiny value of 0.000000007806205 which is, if I've done my math correct around 7.806205 nano-meters/sec so effectively, zero. I recently posted about something similar here if it helps. This is obviously a lot less in magnitude than 0.9 so isn't the source of your issue.

    You have a slightly odd mix of movement code here though. You're using force to adjust the velocity during the simulation but when it comes to rotation you're simply instantly teleporting the transform from one position to the next. You should use either Rigidbody2D.AddTorque or Rigidbody2D.MoveRotation. You should never ever set the transform when using physics components like this. Note that this also breaks interpolation if you're using it.

    If you're not adding force and the body isn't moving much then the body should go to sleep too.
     
    Last edited: Jul 22, 2019
    Callz01 likes this.
  3. Callz01

    Callz01

    Joined:
    Apr 9, 2019
    Posts:
    16
    Thank you! :)
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    No problem at all, glad I could help.
     
    Last edited: Aug 3, 2019
    Callz01 likes this.