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 ! 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerControllerRB : MonoBehaviour {
[Header("Debug Mode Toggle - Draw Line")]
public bool debugOn = true;
[Header("Player Control Attributes")]
public float speedMovement = 10f;
public float speedRotation = 1f;
private float turnY;
private Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody>();
}
void Update() {
DrawDebugRays(debugOn);
}
void FixedUpdate() {
float moveX = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");
Vector3 inputMove = new Vector3(moveX * speedRotation, 0.0f, moveZ * speedMovement);
rb.velocity = transform.forward * inputMove.z; // Add inputMove.z to Rigidbodys forward facing velocity
turnY += inputMove.x; // Rotate the player based on InputMove.x
rb.rotation = Quaternion.Euler(0.0f, turnY, 0.0f); // Apply the rotation
}
void DrawDebugRays(bool active) {
if (active) {
Vector3 forward = transform.TransformDirection(Vector3.forward) * 5;
Debug.DrawRay(transform.position, forward, Color.green); // Draws ray ( length 5 ) so we can see forward direction
}
}
}
You’re explicitly setting the velocity every frame, so your gravity force can’t accumulate.
Vector3 movement = transform.forward * inputMove.z;
movement.y = rb.velocity.y;
rb.velocity = movement;
That should fix it.
2 Likes
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").
1 Like
@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.
rb.velocity = transform.forward * inputMove.z;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerControllerRB : MonoBehaviour {
[Header("Debug Mode Toggle - Draw Line")]
public bool debugOn = true;
[Header("Player Control Attributes")]
public float speedMovement = 10f;
public float speedRotation = 1f;
private float turnY;
private Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody>();
}
void Update() {
DrawDebugRays(debugOn);
}
void FixedUpdate() {
float moveX = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");
Vector3 inputMove = new Vector3(moveX * speedRotation, 0.0f, moveZ * speedMovement);
Vector3 movement = transform.forward * inputMove.z;
movement.y = rb.velocity.y;
rb.velocity = movement;
turnY += inputMove.x; // Rotate the player based on InputMove.x
rb.rotation = Quaternion.Euler(0.0f, turnY, 0.0f); // Apply the rotation
}
void DrawDebugRays(bool active) {
if (active) {
Vector3 forward = transform.TransformDirection(Vector3.forward) * 5;
Debug.DrawRay(transform.position, forward, Color.green); // Draws ray ( length 5 ) so we can see forward direction
}
}
}
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.