Search Unity

Rigidbody Slow To Fall Despite Gravity - Code Issue ?

Discussion in 'Scripting' started by Deleted User, Jan 5, 2018.

  1. Deleted User

    Deleted User

    Guest

    I have a super basic player movement script, I'm applying it to a rigidbody cube and it works well enough, but I have the cube a relatively small amount up from the floor plane, 0.5 on the Y Axis, and even through Gravity is switched on ( everything else just default values ), it slowly falls to the ground, I can only assume it's a problem with my code, any help appreciated ! :)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerControllerRB : MonoBehaviour {
    6.  
    7.     [Header("Debug Mode Toggle - Draw Line")]
    8.     public bool debugOn = true;
    9.  
    10.     [Header("Player Control Attributes")]
    11.     public float speedMovement = 10f;
    12.     public float speedRotation = 1f;
    13.  
    14.     private float turnY;
    15.     private Rigidbody rb;
    16.  
    17.     void Start () {
    18.         rb = GetComponent<Rigidbody>();
    19.     }
    20.      
    21.     void Update() {
    22.         DrawDebugRays(debugOn);
    23.     }
    24.  
    25.     void FixedUpdate() {
    26.         float moveX = Input.GetAxis("Horizontal");
    27.         float moveZ = Input.GetAxis("Vertical");
    28.  
    29.         Vector3 inputMove = new Vector3(moveX * speedRotation, 0.0f, moveZ * speedMovement);
    30.  
    31.         rb.velocity = transform.forward * inputMove.z; // Add inputMove.z to Rigidbodys forward facing velocity
    32.         turnY += inputMove.x; // Rotate the player based on InputMove.x
    33.         rb.rotation = Quaternion.Euler(0.0f, turnY, 0.0f); // Apply the rotation
    34.     }
    35.  
    36.     void DrawDebugRays(bool active) {
    37.         if (active) {
    38.             Vector3 forward = transform.TransformDirection(Vector3.forward) * 5;
    39.             Debug.DrawRay(transform.position, forward, Color.green); // Draws ray ( length 5 ) so we can see forward direction
    40.         }
    41.     }
    42. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You're explicitly setting the velocity every frame, so your gravity force can't accumulate.

    Code (csharp):
    1.  
    2. Vector3 movement = transform.forward * inputMove.z;
    3. movement.y = rb.velocity.y;
    4.  
    5. rb.velocity = movement;
    6.  
    That should fix it.
     
    Rutangaba and Deleted User like this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Code (csharp):
    1. rb.velocity = transform.forward * inputMove.z;
    Note that you set the velocity here. That means any cumulated velocity from acceleration (gravity is an accelerative force) will be lost. So you're effectively resetting the fall velocity.

    Try to maintain the y velocity when you go to move.

    ...

    Do note, despite this. The fall rate may still seem odd depending on the scale of your objects. Again, gravity is accelerative so it starts slow and builds up over time. At scales different to our scale will look weird because it's not what you're used to seeing. If you have gravity set at 9.8 units, you're implying 1 unit is 1 meter. So your 'people' in game should be roughly 1.8 units in height (1.8 meters = about 5'10").
     
    Deleted User likes this.
  4. Deleted User

    Deleted User

    Guest

    @GroZZleR @lordofduct

    Thanks for the help and further explanation of where I'd gone wrong, I've update the code, and removed the offending line. All working. Scale was fine, was testing with standard unity cube, so scale of 1,1,1.
    Code (CSharp):
    1. rb.velocity = transform.forward * inputMove.z;
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerControllerRB : MonoBehaviour {
    6.  
    7.     [Header("Debug Mode Toggle - Draw Line")]
    8.     public bool debugOn = true;
    9.  
    10.     [Header("Player Control Attributes")]
    11.     public float speedMovement = 10f;
    12.     public float speedRotation = 1f;
    13.  
    14.     private float turnY;
    15.     private Rigidbody rb;
    16.  
    17.     void Start () {
    18.         rb = GetComponent<Rigidbody>();
    19.     }
    20.        
    21.     void Update() {
    22.         DrawDebugRays(debugOn);
    23.     }
    24.  
    25.     void FixedUpdate() {
    26.         float moveX = Input.GetAxis("Horizontal");
    27.         float moveZ = Input.GetAxis("Vertical");
    28.  
    29.         Vector3 inputMove = new Vector3(moveX * speedRotation, 0.0f, moveZ * speedMovement);
    30.  
    31.         Vector3 movement = transform.forward * inputMove.z;
    32.         movement.y = rb.velocity.y;
    33.  
    34.         rb.velocity = movement;
    35.  
    36.         turnY += inputMove.x; // Rotate the player based on InputMove.x
    37.         rb.rotation = Quaternion.Euler(0.0f, turnY, 0.0f); // Apply the rotation
    38.     }
    39.  
    40.     void DrawDebugRays(bool active) {
    41.         if (active) {
    42.             Vector3 forward = transform.TransformDirection(Vector3.forward) * 5;
    43.             Debug.DrawRay(transform.position, forward, Color.green); // Draws ray ( length 5 ) so we can see forward direction
    44.         }
    45.     }
    46. }
     
    Last edited by a moderator: Jan 5, 2018
  5. Lets_levi

    Lets_levi

    Joined:
    Sep 5, 2021
    Posts:
    1
    I had a similar problem. It turned out to be a misplaced 2d Box collider within the main camera.
    Since I integrated collision in my tilemaps and player movement, the player character hence collided with the collider, which I missed when I searched for the mistake. The constant collision somehow must have generated a weird sliding motion downwards as well as a not so good replying top down movement.
    After removing the misplaced collider, everything worked completely fine and I didn't had to change my movement script or anything in that direction.
    Even if it wasn't your problem, for those who might struggle with something like this, always look out for colliders in places there shouldn't be.
     
  6. Rutangaba

    Rutangaba

    Joined:
    May 31, 2023
    Posts:
    1
    Thaaanks GroZZleR