Rigidbody slow falling issue

I possibly am making an easy mistake on setting velocity for every fixed update frame but can’t get around of this and need help about to understand what is really going on. When my object starts to falling, it just hangs on the air for a very long time. Here’s my two-piece script attached to the object, thanks in advance.

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

public class p_Control : MonoBehaviour
{
    private p_Module _Playa;
    private Transform _mainCam;
    private Vector3 _mainCamFw;
    private Vector3 _Move;

    void Start()
    {
        if (Camera.main != null){
            _mainCam = Camera.main.transform;
        } else {
            Debug.LogWarning("No main cam found.");
        }
        _Playa = GetComponent<p_Module>();
    }

    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        _Move = Vector3.Normalize(v*Vector3.right + h*Vector3.back);
        //hijacking solution
    }

    void FixedUpdate()
    {
        if (!Input.GetButton("Run")) _Move*= 0.5f;
        _Playa.Move(_Move);
        _Playa.Turn();
        if (Input.GetButtonDown("Fire")) _Playa.Shoot();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class p_Module : MonoBehaviour
{  
    Rigidbody _Rigid;
    [SerializeField] float _mvspd = 100f;
    [SerializeField] float camRayLength = 100f;
    private Vector3 playerToMouse;
    int floorMask;

    void Awake() {
        floorMask = LayerMask.GetMask("Floor");
    }

    void Start()
    {
        _Rigid = GetComponent<Rigidbody>();
        _Rigid.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
    }


    public void Move(Vector3 _Move)
    {
        _Rigid.velocity = (_Move*_mvspd);    
    }

    public void Turn()
    {
        Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit floorHit;
        if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)){
            playerToMouse = floorHit.point - transform.position;
            playerToMouse.y = 0f;
            Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
            _Rigid.MoveRotation(newRotation);
            Debug.DrawRay(transform.position, playerToMouse, Color.green);
        }
    }
   
    public void Shoot()
    {

    }

    void Update()
    {
       
    }
}

Check the drag setting on the Rigibody, set it to zero, see if that was the problem.

Also what happens if you set _mvspd equal to 1000 or 10000? Does anything change? If nothing changes that’s an interesting clue.

1 Like

Thanks for the fast reply Kurt, I set drag to zero but nothing changed. Also changing _mvspd only make my object to go faster on x and z directions but did not change falling speed altogether. I suspect that I’m using fixed update wrong and setting the y velocity on every frame but as I said, I can’t find a solution and understand how it should be.

Oh yeah, good point… that’s the issue: gravity is starting to pull you down and then you are replacing the Y velocity with your zero.

Try taking the .y component out of the velocity, creating a new Vector3 with your intended X/Z speeds, then replace the Y with the value you took out, then assign the entire Vector3 back into .velocity.

NOTE: you cannot directly change individual fields in a Vector3 property such as Rigidbody.velocity.

1 Like

I know it’s a lot to ask but I really don’t get it, can you explain further or maybe an example for it?

    public void Move(Vector3 _Move)
    {
        _Rigid.velocity = new Vector3(_Move.x, _Rigid.velocity.y, _Move.z) * _mvspd;   
    }
1 Like

That doesn’t work due to velocity.y of rigidbody multiplying which causes my object to snap out of scene bounds :slight_smile:
However I think I understood how it’s going to be and changed the script to

        _Rigid.velocity = new Vector3(_Move.x*_mvspd, _Rigid.velocity.y, _Move.z*_mvspd);

…and it works like a charm. Thanks guys.

Oh right, didn’t catch that, lol.

Awesome, and of course you’re welcome.

If you want to sorta see a little more what likely was happening before your fix, check this chart:

See the internal physics update? That’s where the gravity is add (I’m guessing), then in fixed update your original code would reset the y velocity to zero, wiping out the bulk of the gravity-supplied increase.

You can decrease gravity y value from "Project Settings > Physics > Gravity"

Maybe I could’ve increased gravity which I did actually but it changed my other physics-based calculations. The code I posted worked well but I needed a heavier object so I changed it to this and satisfied completely with it now;

        _Rigid.velocity = new Vector3(_Move.x*_mvspd, _Rigid.velocity.y-1, _Move.z*_mvspd);