RayCast hight from Terrain in a walk cycle

Hi, I’m trying to get the lowest float value of a RaycastHit.distance in a walk cycle so I know when the foot is at the lowest position and then I can Instantiate an effect.

I was going to use OnCollisionEnter() with a box collider on the foot but I don’t have or want to put a ridged body on my walker or terrain.

I’m sending out a ray cast from the foot as in the picture below, I’ve tried the below but not with constant results.

#pragma strict
// In the inspector, click on the drop-down menu then tick the layers you wish to be acknowledged
var myLayerMask : LayerMask;
var footDustCloud : Transform;

private var placed : boolean;
private var tr : Transform;
private var hitInfo : RaycastHit;
private var raycastDistance : float;

function Awake () {
	tr = transform;

function Update () {
// Cast a ray to find out the hight of the Mechs foot

	Physics.Raycast (tr.position, -tr.up, hitInfo, 100, myLayerMask);
	Debug.DrawRay (tr.position, -tr.up*10, Color.blue);		

    raycastDistance = Vector3.Distance(transform.position, hitInfo.point);

	if(raycastDistance < 0.9 && !placed){
		placed = true;
		Instantiate(footDustCloud, hitInfo.point, Quaternion.LookRotation(hitInfo.normal));

function reset(){

	while(raycastDistance < 0.9){
	placed = false;

Because as you can see in the second picture the foot does not always land on a flat part of the NavMesh, so raycastDistance < is not always true.

So what would be the best way to achieve this? Always finding the lowest point in the walk cycle?




no need to do a raycast for this. You can mark the position in your animation when the foot touches the ground with curves in the animation.

See this vid at around 37:40 for an example: Unite 12 - Mecanim: Creating Retargetable Animation in Unity 4 - YouTube

Then you just create parameters in your AnimationController with the same name as the curves and you can read the values of the curves with anim.getFloat(“footDown”), so you know when to release your particles.