Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Y velocity going crazy when landing on a surface.

Discussion in 'Getting Started' 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 "Landed" however that never happens because of this issue.

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

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,192
    Uh... 7.806205e-09 is less than 0.9f. Quite a lot less. Almost 0.9f less (as 7.806205e-09 is very very close to zero).

    So I think the problem isn't what you think it is. Go back and focus on why your Debug.Log isn't firing when it should.
     
    Callz01 likes this.
  3. Callz01

    Callz01

    Joined:
    Apr 9, 2019
    Posts:
    16
    Hey, sorry for the late reply. I've figured it out. Thanks!
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    5,448
    What was your issue, for us interested readers? I like your avatar image, by the way :)
     
    Joe-Censored and Callz01 like this.
  5. Callz01

    Callz01

    Joined:
    Apr 9, 2019
    Posts:
    16
    Haha thank you, big fan of Mars! The issue was actually due to a weird rotation bug that happened whenever I land. So I just froze the rotation on the rigidbody component and manually code it through script. And I added a trigger collider slightly above the surface to detect the velocity rather than detecting it through normal collisions which seemed to better. :)
     
  6. DerrickMoore

    DerrickMoore

    Joined:
    Feb 4, 2018
    Posts:
    232
    hey, nice looking Lunar Lander game!
     
  7. Callz01

    Callz01

    Joined:
    Apr 9, 2019
    Posts:
    16
    Thank you! It was for a game jam :)