Question by
TheUltimateTurtleHawk · Apr 03, 2020 at 10:38 PM ·
jumpair
How to add momentum to a character.
How do I make a unity Rigidbody have momentum. In my project, I have a rigidbody that moves freely while it is jumping. As a result, it feels extremely strange controlling the character. My code allows the character to move in all directions freely in the air so there is no momentum. In other games, you can see how the character, if jumping in one direction, cannot be moved easily to go back while still airborne. Here is my code.
[Header("Movement")]
public float Speed;
Rigidbody rb;
[Header("Jumping")]
private float verticalVelocity;
private float gravity = 9.81f;
private float jumpForce = 10.0f;
public bool isGrounded = true;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
PlayerMovement();
if (Input.GetButtonDown("Jump") && isGrounded)
{
rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
isGrounded = false;
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Floor")
{
isGrounded = true;
}
}
void PlayerMovement()
{
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
Vector3 playerMovement = new Vector3(hor, 0f, ver) * Speed * Time.deltaTime;
rb.MovePosition(transform.position + playerMovement);
}
}
Comment
Your answer
Follow this Question
Related Questions
I have a problem with in air movement 0 Answers
Jump raycast not working 0 Answers
Jump problem 1 Answer
Cant jump while sprinting (Unity FPSController) 2 Answers
Character jumping too fast 1 Answer