- Home /
Error in Raycast Detect Ground for Falling Animation
Ive been working on a controller for my game for a couple days now and its been going very smoothly however I ran into this small bug where the fall animation would play when a Ray Cast is determind but the character's Capsule Collider (radius of 0.3) still collides with the wall edge and therefore the model stays on the edge but plays this falling animation.
I have the image here to best explain what goings on: http://gyazo.com/e34f79cfe762f930921ee22f3182cb2e
I know the issue here is that I only have one ray cast that is very slim and since the radius of the character is 0.3 theres a small slight error in which the raycast would not hit the ground thus thinking it should play the animation but the collider still does.
Heres the code:
//grounding checks bool grounded = true; public bool Grounded { get { return grounded; } } private RaycastHit groundHit; public float groundedDistance = 1.5f; // from capsule center public LayerMask groundLayers = -1; private float groundheight = 0.0f;
void Update () {
//check if grounded
grounded = Physics.Raycast (unit.position + unit.up * capcolllider.center.y, unit.up * -1, out groundHit, groundedDistance, groundLayers);
anim.SetBool ("grounded", grounded);
Here is the setup: http://gyazo.com/3d35abc0038370a5e1012d996d06f113
Also I have 3 fall Landing animations that I would like to play when the character hits the ground at particular heights. As of right now I just have one playing but I would like to play the others depending on the height of the fall. If the fall is 0-3 unit meters then play LandA, if 3-10m then play LandB and then greater than that then LandC (which is basically a death falling animation).
I know to achieve this I just need to measure the distance of the Raycast itself but Im not quite sure how, still a little new to raycast functions
Answer by Aggrojag · Jun 06, 2015 at 06:46 AM
An easy fix would be to simply have the wall on a different layermask. With the collider that small you're kind of asking for these types of problems. If a separate layermask if out of the question, perhaps you could also check the tag or name of the hit object.
For measuring distance, I believe RaycastHit.point will get you started. http://docs.unity3d.com/ScriptReference/RaycastHit-point.html
Hopefully this helps. Best of luck.
Alright ill check out that RaycastHit.point
As for the layermask and tag, the reason I am not putting the wall on a seperate mask as the ground is because this was just a test scene where I plan to have floating platforms and the ground is just about nonexistent.
I cant remember but since im using a capsule collider, how would I make it so that the bottom rounded ends are slippery when at an edge? I remember making a system back in Unity 4 but I cant remember my thought process and no code was needed. Perhaps a setting for the capsule collider?