Interpolation with jumping - Acting Weird.

Here is my player controller I have been working on.

Everything works fine, finally found out that using Interpolation for jumping smoothed out the jitteryness of jumping, which thankfully I found.

But now I am running into a stump in the road.

I can still jump fine when I stand still, however, if I start moving, I jump maybe not even half way as high. I tried using Extrapolate or whatever, it was worse than not having Interpolate, but here is my code.

	//private CapsuleCollider col; // This is the Collider for Player.
	private Animator anim;	// This is the Animator for Player.
	public float Speed = 5.0f;
	public float TurnSpeed = 5.0f;
	public float jumpHeight = 10.0f;
	public bool canJump;
	public bool Transition = false;
	// Use this for initialization
	void Start () {
	anim = gameObject.GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () {

				if (!Transition) {
						if (Input.GetKey (KeyCode.W)) {
								transform.Translate (Vector3.forward * Speed * Time.deltaTime);
								anim.SetFloat ("Speed", 1f);
						} else {
								anim.SetFloat ("Speed", 0f);
						}
						if (Input.GetKey (KeyCode.S)) {
								transform.Translate (Vector3.back * Speed * Time.deltaTime);
								anim.SetFloat ("WalkBack", -1f);
						} else {
								anim.SetFloat ("WalkBack", 0f);
						}
						if (Input.GetKey (KeyCode.A)) {
				                transform.Rotate (0,-3,0 * Time.deltaTime);
			}
						if (Input.GetKey (KeyCode.D)) {
								transform.Rotate (0,3,0 * Time.deltaTime);
						}

				}
	}
	
	
	void FixedUpdate(){
	
	if (Input.GetKeyDown (KeyCode.Space) && canJump == true) {
						if (canJump) {
				anim.SetBool ("Jump", true);
				transform.rigidbody.AddRelativeForce (Vector3.up * jumpHeight);
				Physics.gravity = new Vector3 (0, -20, 0);
			}
	}
  }
	void OnCollisionEnter(Collision col){
		if (col.gameObject.tag == "Map") {
			anim.SetBool ("Jump", false);
		}
	}
}

Please, if you can help me, all would be greatly appreciated!

There are three very common pitfalls when using physics in Unity 4.x and lower (that use Nvidia PhysX 2):

  • Altering static colliders.
  • Altering a GameObject that has a Rigidbody without using the Rigidbody members.
  • Altering a Rigidbody outside of FixedUpdate.

Altering means: enabling (i.e. SetActive) or transforming (moving, rotating, scaling). So, whenever there is a GameObject with a Rigidbody, it must always be transformed with the corresponding Rigidbody’s members, and always inside FixedUpdate. I see you are using both Update & FixedUpdate, and Transform & Rigidbody operations.

Make sure of the following:

  • If the collider is loaded/unloaded along with the scene and shouldn’t be altered in any way, it can be a static collider.
  • If the collider is created/destroyed dynamically or is modified in some way, it must be a dynamic collider.

More on static colliders here:

Doing otherwise will corrupt the physics scene and physics operations could fail, until it’s reconstructed, which is expensive and will happen at some unknown point. This is an indirect and accumulative effect that developers might not notice, and if they do, they will have difficulties to understand the cause.

This will be improved in Unity 5, with Nvidia PhysX 3: