Gravity not working when moving

I just added an jump function, and when i jump it works. but if i jump then walk while i am in the air i move, the gravity is much weeker. i am almost floating in the air. it takes like 5 seconds to land after the jump instead of the normal ca 1 second. here is the code. how can i fix it?



6003812--646646--upload_2020-6-20_20-0-16.png

Use code tags to post code rather than screenshots. You are using force for the jump while setting the velocity directly with the move command which is overriding the gravity.

So either store the y value of the rigid body’s velocity, assign the move to a vector, overwrite that vectors y value with the stored y value, manually apply gravity, and then apply that vector to the velocity or use force for the movement as well.

What do you mean “use code tags”. i dont know what that means

The very first post in this forum explains it - Using code tags properly

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public float Speed, SpeedBackwards, SpeedLeft, SpeedRight;
    float inputX, inputY;
    Rigidbody rb;
    public float speedH, speedV = 2.0f;
    private float yaw, pitch = 0.0f;
    float sprint, sprintbackwards;
    float speed1, speedbackwards1;
    public float SprintMultiplyer;
    bool OnGround = true;
    bool jump = false;
    public float JumpForce;

    void Start()
    {
        speed1 = Speed;
        speedbackwards1 = sprintbackwards;
        sprint = Speed * SprintMultiplyer;
        sprintbackwards = SpeedBackwards * 1.5f;
    }

  
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            rb.velocity = transform.forward * Speed;
        }
      
        if (Input.GetKey(KeyCode.S))
        {
            rb.velocity = transform.forward * SpeedBackwards;
        }

        if (Input.GetKey(KeyCode.D))
        {
            rb.velocity = transform.right * Speed;
        }

        if (Input.GetKey(KeyCode.A))
        {
            rb.velocity = transform.right * SpeedBackwards;
        }

        yaw += speedH * Input.GetAxis("Mouse X");
        pitch -= speedV * Input.GetAxis("Mouse Y");
        transform.eulerAngles = new Vector3(0.0f, yaw, 0.0f);

        if(Input.GetKeyDown(KeyCode.LeftControl))
        {
            Speed = sprint;
            SpeedBackwards = sprintbackwards;
            SpeedRight = sprint;
            SpeedLeft = sprintbackwards;
        }

        if (Input.GetKeyUp(KeyCode.LeftControl))
        {
            Speed = speed1;
            SpeedBackwards = speedbackwards1;
            SpeedRight = speed1;
            SpeedLeft = speedbackwards1;
        }

        if (Input.GetButtonDown("Jump"))
        {
            if (OnGround)
            {
                jump = true;
            }
        }


    }

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

    void Jump()
    {
        rb.AddForce(0, JumpForce, 0);
    }

    private void FixedUpdate()
    {
        if (jump)
        {
            Jump();

            jump = false;
        }
    }

    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "Ground")
        {
            OnGround = true;
        }
    }

    void OnCollisionExit(Collision col)
    {
        if (col.gameObject.tag == "Ground")
        {
            OnGround = false;
        }

    }


}

Like this?

I am new to unity and i dont know how to use force for movement. can you explain how i do that?

i have same problem too

i fixed it now it because in rigidbody i turned on Interpolate