I have a default plane and a default capsule with a default rigidbody on it.
The rigidbody has this script on it
Click for code
using UnityEngine;
public class Jump : MonoBehaviour
{
public TestCast testCast;
bool grounded;
Rigidbody myRigidbody;
void Awake()
{
myRigidbody = gameObject.GetComponent<Rigidbody>();
}
void FixedUpdate()
{
myRigidbody.WakeUp();
if(grounded)
{
Debug.Log("FrameCount = " + Time.frameCount + " FixedTime = " + Time.fixedTime + "___ Type = FixedUpdateJump ___ Rigidbody.y = " + myRigidbody.position.y);
myRigidbody.AddForce(Vector3.up * 1000, ForceMode.VelocityChange);
grounded = false;
}
}
void OnCollisionStay(Collision collisionInfo)
{
Debug.Log("FrameCount = " + Time.frameCount + " FixedTime = " + Time.fixedTime + " ___ Type = OnCollisionStay ___ Rigidbody.y = " + myRigidbody.position.y);
testCast.DoRaycast();
grounded = true;
}
}
The “TestCast” script is this
using UnityEngine;
using System.Collections;
public class TestCast : MonoBehaviour
{
public void DoRaycast()
{
RaycastHit hitInfo;
if(Physics.Raycast(transform.position, transform.forward, out hitInfo))
{
Debug.Log("Hit");
}else{
Debug.Log("No Hit");
}
}
}
The rigidbody is to drop, detect the collision, and then jump.
When I hit play, the result is this
Click for image

We see that a collision is detected, a jump is done, and then another collision is detected, but the rigidbody.y is at this point waaay above the floor. Also, by doing a test raycast with an object that is on the floor pointing to where the capsule would be colliding, we see that it detects a hit the first time, but not the second, even though it was being called within the oncollisionstay method.
This tells me that the rigidbody and the collider is being updated, but it seems that either this is a bug, or that maybe OnCollisionStay collision detection is being delayed in some way so that it doesnt actually match what the rigidbody is doing.
Does anyone know if this is the intentional way for things to work?
Physics is so tricky to work with >.>
I included a sample project in case anyone wanted to test it.
P.S. - Please dont give me advise on how to do a jump script. I was only using this as an example.