Using RayCast to Calculate Transforms Height

Hello,

I’m working on some enemy AI, and I’m using a raycast system to determine the Y position of the enemy in comparison to the floor.

What I mean is, basically, the enemy AI fires raycasts downwards, and any thing it hits (floor), the enemy position Y will aline itself with that floor position.

The problem I’m having is that when the enemy walks up a ramp, the raycasts appear to be ‘blocky’. I have included some screen shots to help you guys understand.

alt text

This is the raycast system I use:

var hit : RaycastHit;
	if (Physics.Raycast (raycastPosition.transform.position, -Vector3.up, hit)) {
		distanceToGround = hit.transform.position;
		print("Distance: "+distanceToGround);
		print("Hitting "+hit.transform);
		Debug.DrawLine (raycastPosition.transform.position, hit.point, Color.red, 1);
	}

As I understand, the hit.transform.position only gives the information about the position of the model as a whole. How would I retrieve precise data? I was thinking something like: hit.contacts[0].point, but I get an error.

Any ideas?

Thanks

What I can see from the screenshot, is that your character has a collider that’s bigger than itself. What you’re experiencing is the fact that, since the collider would need to penetrate and intersect the ramp to follow it properly upwards, it’s just ‘bouncing’ out of it.

In simpler words: the bottom right edge of the character collider needs to intersect the ramp to properly follow the height, but the ramp collider is pushing it away, creating that unwanted effect you’re witnessing.
If you rotate the collider, the ramp should be climbed properly.

To find a definitive solution you most probably could just add a sphere collider to the character’s feet, and another collider to the characters’ top.