Question by
kvartira92 · Aug 23, 2020 at 07:02 PM ·
gravityphysics2d
2D planet gravity and Jumping problem
Hello there, I'm trying to simulate a little planet in 2D. A character should run and jump on it. But I have a problem When the player jumps, his local horizontal speed decreases and rotation over the planet becomes slower. I can't handle what forces make the player slow down while he is in the air.
How I add forces to the player:
private void FixedUpdate()
{
//Rotate player to stand straight
playerAngle = (Mathf.Atan2(- _rb.position.x, _rb.position.y) * Mathf.Rad2Deg);
_rb.rotation = playerAngle;
//Add gravity
_rb.AddRelativeForce(Vector2.down * gravityForce, ForceMode2D.Force);
// Add horiontal move
_rb.AddRelativeForce(Vector2.right * horizontalSpeed, ForceMode2D.Force);
if (_jump)
{
_rb.AddRelativeForce(Vector2.up * jumpScale, ForceMode2D.Impulse);
_jump = false;
}
var local_x = _rb.GetRelativeVector(new Vector2(_rb.GetVector(_rb.velocity).x, 0));
Debug.DrawRay(_collider.bounds.center, local_x , Color.red);
Debug.DrawRay(_collider.bounds.center, _rb.velocity , Color.yellow);
Debug.Log(_rb.GetVector(_rb.velocity).x);
}
Some Video demonstration: https://streamable.com/aulfu5
Yellow ray shows applied velocity to the player's rigidbody
Red ray shows x-projection of velocity in local coordinates
Comment