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:
{
Rigidbody2D rb;
[SerializeField]
private float _upwardsForce;
[SerializeField]
private float _fuel;
[SerializeField]
private float _currentVelocity;
[SerializeField]
private Text _fuelText;
[SerializeField]
private Text _velocityText;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
_fuelText.text = "Fuel: " + _fuel.ToString("0.0");
_velocityText.text = "Velocity: " + rb.velocity.ToString("0.000");
_currentVelocity = rb.velocity.magnitude;
//RotateModule();
}
private void FixedUpdate()
{
UpwardsForce();
}
private void UpwardsForce()
{
if (Input.GetKey(KeyCode.W))
{
if (_currentVelocity > 3.5f)
return;
if (_fuel > 0)
{
rb.AddForce(transform.up * _upwardsForce, ForceMode2D.Force);
_fuel -= 0.5f;
}
}
else if (Input.GetKey(KeyCode.A))
{
if (_currentVelocity > 2.5f)
return;
if (_fuel > 0)
{
rb.AddForce(-transform.right * _upwardsForce, ForceMode2D.Force);
_fuel -= 0.2f;
}
}
else if (Input.GetKey(KeyCode.D))
{
if (_currentVelocity > 2.5f)
return;
if (_fuel > 0)
{
rb.AddForce(transform.right * _upwardsForce, ForceMode2D.Force);
_fuel -= 0.2f;
}
}
}
private void RotateModule()
{
if (Input.GetKey(KeyCode.Q))
{
transform.rotation *= Quaternion.Euler(0f, 0f, 1f);
_fuel -= 0.1f;
}
else if (Input.GetKey(KeyCode.E))
{
transform.rotation *= Quaternion.Euler(0f, 0f, -1f);
_fuel -= 0.1f;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Surface"))
{
if (rb.velocity.magnitude < -1.4f)
{
Debug.Log("Safely Landed!");
}
else if (rb.velocity.magnitude > -1.6f)
{
Debug.Log("Crashed!");
}
}
}
}