Physics.raycast acting strange

Hey,

I have a really strange problem with physics.raycast. In the “fall down” section of my code, when I insert

if(hit.collider.gameObject.name != “Cube”){
Debug.Log(“down”);
}

then it won’t run debug, even when raycast clearly is not hitting “Cube”. But when I insert

if(hit.collider.gameObject.name == “Cube”){
Debug.Log(“down”);
}

and raycast hits cube, then it will log “down” and when its not hitting the cube, it will log nothing.

I’m all out of ideas. Please help.

my full code:

using UnityEngine;
using System.Collections;

public class move : MonoBehaviour {

public float step;
public rotate rotate;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	Vector3 rayDirR = transform.right;
	Vector3 rayDirL = -transform.right;
	Vector3 rayDirD = -transform.up;
	float distance = 2;
	RaycastHit hit;
	RaycastHit hitD;
	Ray rayR = new Ray(transform.position, rayDirR);
	Ray rayL = new Ray(transform.position, rayDirL);
	Ray rayD = new Ray(transform.position, rayDirD);

	//Move Left
	if (Input.GetKeyDown (KeyCode.A)) {
		if (Physics.Raycast (rayL, out hit, distance)) {
			if(hit.collider.gameObject.name == "edge"){
				transform.Rotate(new Vector3(transform.rotation.x,90,transform.rotation.z));
				transform.Translate (new Vector3(0,0,-step));
				rotate.rotateL();
			}
		}
		transform.Translate (new Vector3(-step,0,0));
	}

	//Move Right
	if (Input.GetKeyDown (KeyCode.D)) {
		if (Physics.Raycast (rayR, out hit, distance)) {
			if(hit.collider.gameObject.name == "edge"){
				transform.Rotate(new Vector3(transform.rotation.x,-90,transform.rotation.z));
				transform.Translate (new Vector3(0,0,-step));
				rotate.rotateR();
			}
		}
		transform.Translate (new Vector3(step,0,0));
	}

	//Fall down
	if (Physics.Raycast (rayD, out hit, distance)) {
		Debug.Log(hit.collider.gameObject.name);
		if(hit.collider.gameObject.name != "Cube"){
			Debug.Log("down");
			//transform.Rotate(new Vector3(transform.rotation.x,-90,transform.rotation.z));
			transform.Translate (new Vector3(0,-step,0));
			//rotate.rotateR();
		}
		else{
			Debug.Log("up");
		}
	}

	Debug.DrawRay(rayR.origin, rayR.direction * distance, Color.yellow);
	Debug.DrawRay(rayL.origin, rayL.direction * distance, Color.green);
	Debug.DrawRay(rayD.origin, rayD.direction * distance, Color.blue);
}

}

This is probably happening because the raycast is actually not hitting anything. In the code:

if (Physics.Raycast (rayD, out hit, distance)) {
        Debug.Log(hit.collider.gameObject.name);
        if(hit.collider.gameObject.name != "Cube"){
            Debug.Log("down");
            //transform.Rotate(new Vector3(transform.rotation.x,-90,transform.rotation.z));
            transform.Translate (new Vector3(0,-step,0));
            //rotate.rotateR();
        }
        else{
            Debug.Log("up");
        }
    }

The if(Physics.Raycast(…))… is actually checking if the raycast hits anything, and only if the raycast hits something will anything inside the block of code execute.

If this helps:

if (Physics.Raycast (rayD, out hit, distance))
{
    //If the raycast hits something.
}
else
{
    //If the raycast does not hit something.
}