RigidBody character problems (constant floating, jumping, groundedtest)

I have made a character, using a rigidbody to control his movements. The movement works fine, and the gravity works almost fine as well. However, there are some issues I have no idea why occur.

Constantly, the character will float steadily upwards, unless I check the “Freeze Position Y” boolean. Also, my jumping is not working as it should. I press spacebar while he is grounded, and even with 1000 in force he will just take off a little bit and fall down again. Furthermore, on the weird part, I made grounded test function - but when I go from my starting platform to one below (just dropping down, he will stand high above the object and claim to be grounded

[24349-floating+character.jpg|24349]

As you can see he is hovering above the ground, while claiming he is grounded.

So my problems are the following:

  • My character keeps floating up
  • My jump function is horrible, and I have no idea what else to do at this moment
  • My grounded test function makes the character float above objects, which is silly.

I have included the script that controls my characters movement.

using UnityEngine;
using System.Collections;

public class PlayerMotor : MonoBehaviour {


	protected Animator anim;

	private HashID hash;

	public float DirectionDampTime = 0.25f;
	public float turnSmoothing = 15f;
	public float speedDampTime;
	public float speed;
	public float gravity = 20.0f;
	public float jumpForce = 20.0f;

	private float distToGround;

	void Awake(){
		anim = this.GetComponent<Animator>();
		hash = GameObject.Find ("Game Controller").GetComponent<HashID>();
	}

	void Start(){
		distToGround = collider.bounds.extents.y;
	}

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

		if(Input.GetKey(KeyCode.Space) && IsGrounded()){
			print ("jumped");
			Jump();
		}
		MovementMangemant(new Vector3(v, 0, h));
		print (IsGrounded());
	}

	void MovementMangemant(Vector3 targetDirection){
		if(targetDirection.x != 0f || targetDirection.z != 0f){
			Rotation(targetDirection.z, targetDirection.x);
			anim.SetFloat(hash.speedFloat, 5f, speedDampTime, Time.deltaTime);
			anim.speed = 5f;
		}else{
			anim.SetFloat(hash.speedFloat, 0f);
			anim.speed = 1f;
		}
		Vector3 newPosition = Vector3.Lerp(rigidbody.position, rigidbody.position+targetDirection, speed * Time.deltaTime); 
		rigidbody.MovePosition(newPosition);
		if(!IsGrounded()){
			targetDirection = new Vector3 (0, -1, 0);
			newPosition = Vector3.Lerp(rigidbody.position, rigidbody.position+targetDirection, gravity * Time.deltaTime);
			rigidbody.MovePosition(newPosition);
		}
	}

	void Rotation(float horizontal, float vertical){
		Vector3 targetDirection = new Vector3(vertical, 0f, horizontal);
		Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
		Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
		rigidbody.MoveRotation(newRotation);
	}

	void Jump(){
		rigidbody.AddForce(Vector3.up * jumpForce);
	}

	bool IsGrounded(){
		return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
	}
}

I had trouble with my character floating too and google brought me here. I noticed my character wouldn’t float when I was using an idle animation from the standard assets so I compared the idle asset I was using with the standard one and found my idle animation did not have “bake into pose” checked under Animations section. I checked all three and it fixed my problem. Here’s a screen:

59469-unityscreen.png

Hi there

Few things. Do you have gravity turned off in the physics engine itself? It looks like you’re trying to do your own gravity. If that’s the case, as soon as your charcter gets any upwards velocity he’s gonna just keep drifting that way, as you never explicitly set the characters velocity to be flat again. I would enable gravity in the physics engine and avoid trying to do your own gravity.

In terms of the movement, I would do it by fiddling with the rigid body’s velocity rather than using MovePosition. You simply need to take the velocity and set it’s x/z components to make it move in the x/z plane, leaving the y alone so gravity can do its job.

The grounded test I would imagine is failing because doesn’t filter out the player, so you’re probably just finding the player each time. Try extending the IsGrounded function to do a ray cast and get the RayCastHit structure, then Debug.Log the thing it hits (hit.collider.name). I suspect you’ll find it doesn’t print out the name of the ground object.

For the jump, once you’ve switched to using normal gravity I suspect it’ll work fine, however a force of even 1000 isn’t that low if the character has a mass of around 1. Forces are scaled by both the mass of the object AND the time step, so a force of 1000 on a mass of 1 at time step 1/30 gives an accleration of just 33, which is killed off pretty quickly under gravity. Try using an impulse force (independent of time - good for 1 shot forces that you don’t want affected by physics time step), or even an acceleration force (independent of both time AND mass).

In summary:

  • use the built in gravity, not your own custom gravity (although if you do want your own custom gravity, do it by adding an acceleration force downwards each frame - thats what gravity actually is)
  • do your movement by setting the xz components of the characters velocity, not the position of the character.
  • make sure your IsGrounded test is actually returning the ground. If it isn’t you can filter ray casts by layer (search for LayerMask)
  • if jumping still doesn’t work, try just adding an acceleration force, as that’s what you’re really after - a 1 time change in velocity regardless of the characters mass or time step

Hope that helps :slight_smile:

I know this post was 4 years ago, but, I had the same problem.
If you check Edit > Project Settings > Physics, check the ‘y’ value of the Gravity. -9.81 is a good setting (mimicing Earth’s gravity).
My player would go up hills but then not back down, and would start floating upwards if I jumped while on a slope, or if I went up a particularly steep area.
I solved this by ticking the ‘Is Walking’ box on my First Person Controller, and setting the Gravity Multiplier to 1. My Rigidbody component had ‘Use Gravity’ and ‘Is Kinematic’ ticked, with no Constraints ticked
I’ve included a snip of my Inspector. Note that the ‘Apply Root Motion’ box is also unchecked.

Hope this helps someone!

Thanks a lot :slight_smile: I found out that my Animator had checked “Apply Root Motion” which interfered with the gravity on RigidBody. Unchecking it made the gravity work, so I removed my own. Also I found that my raycast did actually hit the ground (at least after I used the normal gravity from the engine) However, my collider on my character had to be increased a bit, else the raycast would shoot past the platforms when walking on them.

The jumping also works now, I just have to fine tune it :slight_smile:

Bake into pose was exactly my issue. I couldn’t figure out what the hell was going on.