I'm new to using unity and came across a problem when I was following a tutorial. The game is in two dimensional space, and when my player walks over a certain tile the detection never goes off. Here's the code that I have,

function calculateWalk() {

    yield WaitForSeconds(0.3);

    var hit : RaycastHit;

    if(Physics.Raycast (transform.position, Vector3.down, 100.0)) { // Never evaluates to being true
        var distanceToGround = hit.distance;

        if(hit.collider.gameObject.tag == "tallGrass") {
            Debug.Log("Tall Grass");
        } // END if
    } // END if
} // END calculateWalk()

I've made sure to attach the script to my player, as well as tag the tiles that I want with "tallGrass". I've followed what was done in the tutorial, but for some reason it's not working out for me, not sure if this is all the code needed to help solve this problem, if more information is needed let me know, I also set my player 1 unit above the tiles.

  • \$\begingroup\$ Use the OnDrawGizmos() method and debug your code by drawing a line in the position of your ray... docs.unity3d.com/ScriptReference/… that way you can see where your ray is pointing because you may have switched direction vectors or something... Who knows!! But we have to learn to solve our own problems sooner or later :) \$\endgroup\$
    – Savlon
    Jun 15, 2014 at 0:03

1 Answer 1


You are not calling the right Physics.Raycast as well - you would need to pass the RaycastHit object into the function as an output variable.

I'm suprised the code is even compiling, since you aren't initializing the RaycastHit, and then using it when it shouldn't have any value assigned to it.

You should also use the 'default' distance of infinity just in case 100 isn't enough

Debug.Log("Testing below me");
var hit : RaycastHit;
//Use the overload for Physics.Raycast that allows a RaycastHit object to be an output.
if (Physics.Raycast(transform.position, Vector3.down, hit)) {
    Debug.Log("I'm standing on " + hit.collider);

Also, remove the 'yield' statement. That part of the code past the yield might never be getting run.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .