Using rb.velocity causes low gravity.

I have a player controller script for controlling the player, however I ran into a snag when I tried using this line of code to control the player.

rb.velocity = new Vector3 (Time.deltaTime * h * speed, 0, Time.deltaTime * v * speed);

Where rb is a public Rigidbody, h is the horizontal input from the player, v is the vertical input of the player and speed is a public float that is not 0.

As soon as I disable this line of code, the gravity on this object returns to normal, other objects in the scene are unaffected gravity-wise.

Even if I use:

	rb.velocity = new Vector3 (0, 0, 0);

The problem still persists and adding a negative number in the y velocity to “increase” gravity is not useful either since if I do that, my movement speed on the ground is much slower then in the air. I also can’t just remove it because I need to control the player with it.

This is the full contents of the player controller script:

public float speed;
public float sensitivity;
public float Jump;
public Rigidbody rb;

void Start()
{

	rb = GetComponent<Rigidbody> ();

}

// Update is called once per frame
void Update () {

	//float h = Input.GetAxisRaw ("Horizontal");
	//float v = Input.GetAxisRaw ("Vertical");
	float mh = Input.GetAxis ("Mouse X");

	//transform.Translate (Vector3.forward * Time.deltaTime * v * speed, Space.Self);
	//transform.Translate (Vector3.right * Time.deltaTime * h * speed, Space.Self);

	transform.Rotate (0, mh * sensitivity * Time.deltaTime, 0);
}

void FixedUpdate()
{
	bool j = Input.GetButtonDown ("Jump");
	float h = Input.GetAxisRaw ("Horizontal");
	float v = Input.GetAxisRaw ("Vertical");

	if (j) {
	
		rb.AddForce (transform.up * Jump);

	}

	rb.velocity = new Vector3 (Time.deltaTime * h * speed, Time.deltaTime * 0, Time.deltaTime * v * speed);

}

I think you should use rb.addForce instead. I think, that if you use velocity, you also automatically overwrite the gravity (correct me if I’m wrong). If this is a useless answer, or just downright wrong, just ignore it!

Kind regards,

Rens van Breukelen

I was able to use rb.AddRelativeForce as I was using before I was using rb.velocity, however the problem I was having originally that made switch to rb.velocity in the first place was that the player would have very slippery controls because I was applying forces to it. I added a line of code to reset the x and z velocity every frame to fix this issue, right after the movement forces were applied, like this:

rb.AddRelativeForce(Time.deltaTime * h * speed, 0, Time.deltaTime * v * speed, ForceMode.VelocityChange);
rb.velocity = new Vector3(0, rb.velocity.y, 0);

As it turns out though now that I’m looking at it and learned more about rb.velocity (because I had to use it to reset the x and z velocity), I’m pretty sure the issue would be resolved by simply replacing that last line of code i.e:

rb.velocity = new Vector3 (Time.deltaTime * h * speed, Time.deltaTime * 0, Time.deltaTime * v * speed);

with:

rb.velocity = new Vector3 (Time.deltaTime * h * speed, rb.velocity.y, Time.deltaTime * v * speed);

Which is probably simpler, here is the new version of my player controller script that now works perfectly fine, with normal gravity, proper collision and no slippery controls. Note that I had to bump up the speed value to compensate for the velocity I removed.

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

public class PlayerController : MonoBehaviour {

	public float speed;
	public float sensitivity;
	public float Jump;
	public Rigidbody rb;

	void Start()
	{

		rb = GetComponent<Rigidbody> ();

	}

	// Update is called once per frame
	void Update () {

		//float h = Input.GetAxisRaw ("Horizontal");
		//float v = Input.GetAxisRaw ("Vertical");
		float mh = Input.GetAxis ("Mouse X");

		//transform.Translate (Vector3.forward * Time.deltaTime * v * speed, Space.Self);
		//transform.Translate (Vector3.right * Time.deltaTime * h * speed, Space.Self);

		transform.Rotate (0, mh * sensitivity * Time.deltaTime, 0);
	}

	void FixedUpdate()
	{
		bool j = Input.GetButtonDown ("Jump");
		float h = Input.GetAxisRaw ("Horizontal");
		float v = Input.GetAxisRaw ("Vertical");

		if (j) {
		
			rb.AddForce (transform.up * Jump, ForceMode.VelocityChange);

		}

		rb.AddRelativeForce(Time.deltaTime * h * speed, 0, Time.deltaTime * v * speed, ForceMode.VelocityChange);
		rb.velocity = new Vector3(0, rb.velocity.y, 0);

	}
}