I am using raycast to detect whether a rigidbody is on the ground. When I play the game initially, the body is in the air and the raycast detects that it is not hitting anything, however once the body has hit the ground, if it leaves the surface again the raycast still returns a hit. I have lowered the length of the raycast as much as I can and can see in the play preview that the ray is not hitting anything. I have also narrowed it down to one particular collider but no dice.. Here is the code -
function FixedUpdate() {
Debug.DrawRay (transform.position, -Vector3.up * 0.07);
var hit : RaycastHit;
var rayDown = transform.TransformDirection(-Vector3.up);
if (Physics.Raycast (this.transform.position, rayDown, hit, 0.07)) {
if(hit.collider.tag == "floor"){
hitnormal = hit.normal;
hitting = true;
print("hitting floor"); //this prints once the body hits the ground initially
}else { //close ifTwo / open else
print("no hit"); //this never prints
hitting = false;
}
}
myDirection = transform.TransformDirection(Vector3.forward);
var temp : Vector3 = Vector3.Cross(hitnormal, myDirection);
var myDirection : Vector3 = Vector3.Cross(temp, hitnormal);
//forward vector relative to camera
var forward=transform.TransformDirection(Vector3.forward);
//reset y to walk horizontally, so that camera looking up doesnt matter
forward.y=0;
//normalize to apply speed later
forward=myDirection.normalized;
//right vector relative to the camera, to strafe
var right=Vector3(forward.z, 0, -forward.x);
//make the player always look forward relatively to the camera
transform.rotation=Quaternion.LookRotation(forward);
//controls when grounded / not flying
if (hitting) {
var myTurn = Input.GetAxis("Horizontal")*Time.deltaTime*turnSpeed;
var myStrafe = Input.GetAxis("Horizontal")*Time.deltaTime*strafeSpeed;
transform.Rotate(Vector3.up * myTurn);
transform.Translate(Vector3.right * myStrafe);
var myFwd = transform.TransformDirection(Vector3.forward);
rigidbody.AddRelativeForce(0,0,50);
//this next bit works initially before the player hits the ground
//but not after it has hit the ground once
} else if (!hitting) {
var noTurn : float = 0.0;
var noStrafe : float = 0.0;
transform.Rotate(Vector3.up * noTurn);
transform.Translate(Vector3.right * noStrafe);
rigidbody.AddRelativeForce(0,0,0);
print("not");
}
I am really new to this so would really appreciate some help - thank you :)