Raycasting to check what object (with a particular tag) is underneath the Player

(Player Object is Using the CharacterController component)

This seems like the best way (unless you can do it vectors). I want it to where depending on what type of surface you're on, particles emit to indicate it. (i.e: dust if your on a dirt terrain, grass if on a grass terrain) and to play a sound for the corresponding textures while there verticalDirection > 0. I figured since I can't use triggers or colliders for this, I should use raycasting.

However, I'm a complete novice to raycasting and don't know how I could execute this. I was also wondering if it would be possible to check the texture of the object I'm standing on in case on object that has multiple textures.

Edit: I made a script based on lhk's answer, but I'm getting a error: Unknown Identifier: "origin".

Here is the code:

var dirtPuff : Transform;
var timeOut = 1.5;
var hitinfo : RaycastHit;

    function Update()
        //RaycastHit hitinfo;

            if (hitinfo.transform.tag == "Dirt") 
               Debug.Log("Hit Dirt");
               var clone;
               clone = Instantiate(dirtPuff, hitinfo.transform.position, hitinfo.transform.rotation);
               Destroy(clone.gameObject, timeOut);
               //Instantiate (water_solider_impact);

The reference says:

static function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool 

As you can see you call Physics.Raycast() with the origin and the direction as parameters. A RaycastHit object, the distance/length of the ray and a layer are optional parameters. Physics.Raycast() returns true, if something was hit. To sum up you need to

  1. Get the origin of the Ray. For example, the bottom of your player object
  2. Get the direction of the Ray. Vector3.down should fit your needs.
  3. Create a RaycastHit object as additional parameter
  4. Choose the length of the Ray

    RaycastHit hitinfo;



    //something was hit. Now you can get information about the object that was hit by
    //reading the values of hitinfo


RaycastHit contains

  1. point

    The impact point in world space where the ray hit the collider.

  2. normal

    The normal of the surface the ray hit.

  3. barycentricCoordinate

    The barycentric coordinate of the triangle we hit.

  4. distance

    The distance from the ray's origin to the impact point.

  5. triangleIndex

    The index of the triangle that was hit.

  6. textureCoord

    The uv texture coordinate at the impact point.

  7. textureCoord2

    The secondary uv texture coordinate at the impact point.

  8. lightmapCoord

    The uv lightmap coordinate at the impact point.

  9. collider

    The Collider that was hit.

  10. rigidbody

    The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null.

  11. transform

    The Transform of the rigidbody or collider that was hit.

By using


You may access the gameObject the ray has hit. Then you can check wether it is tagged "surface" or something like that. Moreover the hitinfo offers you the desired Texture coordinates.

There is no variable named origin in your script. Nevertheless you try to access it in the call ti Physics.Raycast. You could add another line to the Update function, before Physics.Raycast

var origin : Vector3 = transform.position-Vector3.up;

Note that I'm substracting another Vector of your position. This is meant to avoid intersection from the Ray with the player itself. I don't know wether this is necessary.