Custom gravity and Velocity

Hi, I am trying to achieve realistic movements on my object for which I have been writing a custom gravity (with gravity Switched-Off on the RigidBody Component) and velocity script, but my gravity does not act properly. Even if I Switch-On the gravity in the RigidBody Component, My gravity and jump do not work.
My gravity(Scripted or from the editor) and Jump seems to work properly when I do not implement Movements. I have also tried reducing Drag below zero and yet my gravity and jump do not work. Here are the Scripts -

Physics.cs -

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

namespace CustomPhysics
{
    public class PhysicsClass : MonoBehaviour
    {
        private static PhysicsClass _physicsInstance;

        public static PhysicsClass PhysicsInstance
        {
            get
            {
                if (_physicsInstance == null)
                {
                    _physicsInstance = GameObject.FindObjectOfType<PhysicsClass>();
                }
                return _physicsInstance;
            }
        }

        //gravity fields
        private Vector3 _gravityVector;
        private float _jumpAcceleration;
        private float accelerationDueToGravity;

        //movement fields
        private float moveAcceleration;
        private float friction;
        private Vector3 _moveVector;

        private void Awake()
        {
            moveAcceleration = 5;
            friction = 1.5f;
            accelerationDueToGravity = 9.8f;
        }

        public float JumpAcceleration
        {
            get
            {
                _jumpAcceleration = 3;
                return _jumpAcceleration;
            }
        }

        public Vector3 GravityVector
        {
            get
            {
                _gravityVector = Vector3.down * accelerationDueToGravity;
                return _gravityVector;
            }
        }

        public Vector3 MoveVector
        {
            get
            {
                float x = Input.GetAxis("Horizontal");
                float y = Input.GetAxis("Vertical");
                _moveVector = (new Vector3(x, 0, y) * moveAcceleration) / friction;
                return _moveVector;
            }
        }
    }
}

GravityAndVelocity.cs -

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static CustomPhysics.PhysicsClass;

public class GravityAndVelocity: MonoBehaviour
{
    private Rigidbody RB;
    private Transform objectTransform;
    private float heightFactor;

    private void Start()
    {
        RB = gameObject.GetComponent<Rigidbody>();
        objectTransform = gameObject.GetComponent<Transform>();
        heightFactor = 1.75f;
    }

    private void FixedUpdate()
    {
        //RB.drag = -100; 

        if (objectTransform.position.y > 0.1f)
        {
            gravity();
            if (Input.GetKey(KeyCode.Space) && objectTransform.position.y < heightFactor)
            {
                RB.velocity = Vector3.up * PhysicsInstance.JumpAcceleration; // * Time.time;
            }
        }

        RB.velocity = (PhysicsInstance.MoveVector);
    }

    void gravity()
    {
        RB.AddForce(PhysicsInstance.GravityVector);
    }
}

So, What have I been doing wrong? Please, kindly Explain.

Whenever you set RB.velocity to a value you will overwrite it’s current velocity and any other change you might do or have done to the RB’s velocity. That means when you do:

RB.velocity = (PhysicsInstance.MoveVector);

You remove all vertical momentum the object might have had. Likewise when you do:

RB.velocity = Vector3.up * PhysicsInstance.JumpAcceleration; 

you also replace the velocity completely. So all momentum (horizontal or vertical) will be removed and you only set an impulse upwards. This of course is pretty irrelevant since immediately after the jump code you overwrite the velocity again with the first line I’ve mentioned.

You want to preserve the y velocity when you set your horizontal movement. Something like this:

float ySpeed = RB.velocity.y;
if (objectTransform.position.y > 0.1f)
{
    gravity();
    if (Input.GetKey(KeyCode.Space) && objectTransform.position.y < heightFactor)
    {
        RB.velocity += Vector3.up * PhysicsInstance.JumpAcceleration;;
    }
}
RB.velocity = (PhysicsInstance.MoveVector) + Vector3.up*ySpeed;

Note that mixing directly setting the velocity and using AddForce might cause some unexpected results. The acceleration you get due to AddForce will be applied to the velocity the next physics frame.

Actually I don’t quite understand the point of your singleton “PhysicsClass”. Why don’t you just use Unity’s Physics parameter? For example if you want to change the gravity vector, you can directly change Physics.gravity. Also note that your “moveAcceleration” is not an acceleration at all so the variable name is misleading. This is your actual move velocity. Also having your “friction” hardcoded in your input vector dosen’t seem to make any sense. It does not apply any friction. It just does a fix lowering of your input movement vector. Overall this seems really strange to me.