Unity Character Falls Slowly
Hi!
When my character jumps, his fall is really floaty... I made a video of this happening.
I don't understand why this is happening since I don't have anything modifying his upward velocity except for in the Jump() method.
The gravity is -9.81, the default value. Yet I've never had this "slow fall" problem with my other Unity projects, therefore default gravity should be fine.
Here is the entire script... Including my normal movement.
I will also include a shorter version of it that only looks at the jumping.
Movement + Jump Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
[Header("Movement")]
[SerializeField]
private bool _forward;
[SerializeField]
private bool _backward;
[SerializeField]
private bool _left;
[SerializeField]
private bool _right;
[SerializeField]
private bool _run;
private Rigidbody _rbody;
public float MaxRunSpeed = 50f, MaxWalkSpeed = 20f;
public float WalkSpeed = 30f, RunSpeed = 60f;
[Header("Jumping")]
[SerializeField]
private bool _jump;
[SerializeField]
private bool _isGrounded;
public float JumpForce = 10f;
// Use this for initialization
void Start()
{
_rbody = GetComponent<Rigidbody>();
_rbody.constraints = RigidbodyConstraints.FreezeRotation;
}
private void FixedUpdate()
{
// Collect the input for each movement key
_forward = Input.GetKey(KeyCode.W);
_backward = Input.GetKey(KeyCode.S);
_left = Input.GetKey(KeyCode.A);
_right = Input.GetKey(KeyCode.D);
_run = Input.GetKey(KeyCode.LeftShift);
_jump = Input.GetKey(KeyCode.Space);
// If pushing the forward key
if (_forward)
{
// Move forward
Move(transform.forward);
}
// If pushing the backward key
if (_backward)
{
// Move backward
Move(-transform.forward);
}
// If pushing the left key
if (_left)
{
// Move left
Move(-transform.right);
}
// If pushing the right key
if (_right)
{
// Move right
Move(transform.right);
}
// If we're grounded
if (_isGrounded)
{
// If pushing jump button
if (_jump)
{
// Jump!
Jump();
}
}
// If we're in the air
else
{
// If our velocity is smaller or equal to 0
if(_rbody.velocity.y <= 0)
{
// Then we're falling
Debug.Log("Falling");
}
}
}
/// <summary>
/// Moves the player in the passed in direction.
/// </summary>
/// <param name="direction">The direction to move in.</param>
private void Move(Vector3 direction)
{
float moveSpeed = 0f;
float maxSpeed = 0f;
// If we're running
if(_run)
{
moveSpeed = RunSpeed;
maxSpeed = MaxRunSpeed;
}
// If we're not running
else
{
moveSpeed = WalkSpeed;
maxSpeed = MaxWalkSpeed;
}
// Add force in the given direction
_rbody.AddForce(direction * moveSpeed);
// Clamp our velocity so we don't go too fast
_rbody.velocity = Vector3.ClampMagnitude(_rbody.velocity, maxSpeed);
}
/// <summary>
/// Makes the player jump in the air using forces.
/// </summary>
private void Jump()
{
_rbody.AddForce(transform.up * JumpForce);
_isGrounded = false;
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Ground")
{
_isGrounded = true;
}
}
}
Jump Code Only
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
[SerializeField]
private bool _jump;
[SerializeField]
private bool _isGrounded;
public float JumpForce = 10f;
private void FixedUpdate()
{
// If we're grounded
if (_isGrounded)
{
// If pushing jump button
if (_jump)
{
// Jump!
Jump();
}
}
// If we're in the air
else
{
// If our velocity is smaller or equal to 0
if(_rbody.velocity.y <= 0)
{
// Then we're falling
Debug.Log("Falling");
}
}
}
/// <summary>
/// Makes the player jump in the air using forces.
/// </summary>
private void Jump()
{
_rbody.AddForce(transform.up * JumpForce);
_isGrounded = false;
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Ground")
{
_isGrounded = true;
}
}
}
Could someone please tell me why my character is falling slowly?
Your answer
Follow this Question
Related Questions
Player (Using Player Controller) slow falls when I repeatedly press the jump button while falling., 0 Answers
Bugs on move in air(falling); 0 Answers
Is there a way to prevent camera from falling even though it is a child of the player? 0 Answers
Decreasing Gravity Under Certain Conditions Problem 0 Answers