(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(Physics.Raycast(origin,Vector3.down,hitinfo,2))
{
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);
}
}
}
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
Get the origin of the Ray. For example, the bottom of your player object
Get the direction of the Ray. Vector3.down should fit your needs.
Create a RaycastHit object as additional parameter
//something was hit. Now you can get information about the object that was hit by
//reading the values of hitinfo
}
RaycastHit contains
point
The impact point in world space
where the ray hit the collider.
normal
The normal of the surface the ray
hit.
barycentricCoordinate
The barycentric coordinate of the
triangle we hit.
distance
The distance from the ray's origin
to the impact point.
triangleIndex
The index of the triangle that was
hit.
textureCoord
The uv texture coordinate at the
impact point.
textureCoord2
The secondary uv texture coordinate
at the impact point.
lightmapCoord
The uv lightmap coordinate at the
impact point.
collider
The Collider that was hit.
rigidbody
The Rigidbody of the collider that
was hit. If the collider is not
attached to a rigidbody then it is
null.
transform
The Transform of the rigidbody or
collider that was hit.
By using
hitinfo.collider.gameObject
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.