Need Help With Rigidbody Gravity

Hey guys! I’m semi-new to Unity and I’m definitely a beginner when it comes to scripting, so please be gentle if there is something very obviously wrong xD. For the past couple of days, I’ve been trying to figure out how to add normal looking gravity to my character so that it gains speed as it falls, rather than just floating down. I been trying to achieve this using rigidbody.addForce, but everything that I have tried so far has not been successful. The closest I got was being able to just up the gravity value, but I couldn’t figure out how to do it over time (if that makes sense). My brain is fried and I’ve searched, read, followed and tried to learn from so many different sources to no avail. Hopefully one of you can help explain this to me, here’s the code as is:

using UnityEngine;

public class PlayerJump : MonoBehaviour
{

    public float jumpSpeed = 100f;
    public float jumpCount = 0f;
    public float jumpMax = 3f;
    private Rigidbody rb;
    private Collision collision;
    private float verticalVelocity;
    public float gravity = 14f;
   


    public bool isGrounded;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();     
    }


    void OnCollisionEnter()
    {
        isGrounded = true;
        jumpCount = 0f;
    }


    void FixedUpdate()
    {
        if (isGrounded == true)
        {
            verticalVelocity = -gravity * Time.deltaTime;

            if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
            {
                rb.AddForce(new Vector3(0, 1, 0) * jumpSpeed, ForceMode.Impulse);
               
                jumpCount++;

                if (jumpCount == jumpMax)
                {
                    isGrounded = false;
                }

                else
                {
                    verticalVelocity -= gravity * Time.deltaTime;
                }

               

            }

            Vector3 fallSpeed = new Vector3(0, verticalVelocity, 0);
            rb.AddForce(fallSpeed, ForceMode.Acceleration);

        }
    }


   



}

and here’s the PlayerMotor:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour {

    [SerializeField]
    private Camera cam;

    private Vector3 velocity = Vector3.zero;
    private Vector3 rotation = Vector3.zero;
    private float cameraRotationX = 0f;
    private float currentCameraRotationX = 0f;

    [SerializeField]
    private float cameraRotationLimit = 85f;

    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    //Gets rotational vector
    public void Move (Vector3 _velocity)
    {
        velocity = _velocity;
    }

    //Gets movement vector
    public void Rotate(Vector3 _rotation)
    {
        rotation = _rotation;
    }

    //Gets movement vector for
    public void RotateCamera(float _cameraRotationX)
    {
        cameraRotationX = _cameraRotationX;
    }

    // Run every physics iteration
    void FixedUpdate()
    {
        PerformMovement();
        PerformRotation();
    }

    //Perform movement based on velocity
    void PerformMovement()
    {
        if(velocity != Vector3.zero)
        {
            rb.MovePosition(transform.position + velocity * Time.fixedDeltaTime);
        }
    }

    //Perform rotation
    void PerformRotation()
    {
        rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
        if(cam != null)
        {
            currentCameraRotationX -= cameraRotationX;
            currentCameraRotationX = Mathf.Clamp(currentCameraRotationX, -cameraRotationLimit, cameraRotationLimit);

            cam.transform.localEulerAngles = new Vector3(currentCameraRotationX, 0, 0f);
        }
    }

}

If you need more details, please tell me… I am just soooo stuck on this and I’m sick of running in circles trying to get this to work on my own.

Check the drag setting on your Rigidbody. If it is high it will damp your motion and make you fall at near-constant speed even when in gravity.

Try set the drag to zero and you will see how the gravity affects you. Constant gravity will cause you to steadily accelerate the further you fall.

1 Like

Damn… Of course, I’d overlook something that stupidly simple… Thanks for the help :smile:

1 Like