- Home /
Best solution for knowing when youre falling and for fall damage
This is how i have it scripted, when you leave the ground it ray casts your distance from the ground, and when you land, if you fell from too far it inflicts damage, the problems i am facing and the questions i have are: Q: is this the best solution? ive also put the code for collision stay on collision enter with same results
the problem i have is that the character constantly gets read, ad if its off the ground, so he flickers a bit into the falling animation alot, and also when he does fall no matetr from what hieght, he sees to be hurt too much, alot of times its when walking over a little thing that he triggers falling mode, and gets inflicted, is this due to the object being partly beneathe the ground, and the raycast going through it, to beyond the terrain? thats what i feel is happening, but as far as my scripting ability goes, this is the best i can do without help, if any one knwos how to help, id greatly apprciate it, as for now, the code is commented out and unused, till the problems are solved
/*
function OnCollisionExit(collision : Collision) {
if(collision.gameObject.name == "Terrain" || collision.gameObject.tag == "walkable"){
if(horsed == 1) { AnimationPlay("Horsefalling",1); }
if(horsed == 0) { AnimationPlay("falling",1); }
nextHit = Time.time + 1;
if(nextHit1 < 1){
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.down) , hit, Mathf.Infinity)){
distToGround = hit.distance;
nextHit = Time.time + 2;
isgrounded = false;
}
}
}
}
*/
function OnCollisionStay (collision : Collision) {
for ( var contact : ContactPoint in collision.contacts ) {
/*
if (contact.otherCollider.name != "Terrain" && contact.otherCollider.tag != "walkable") {
StopPlayerCertainly();
return;
}
if (contact.otherCollider.name == "Terrain" || contact.otherCollider.tag == "walkable") {
if (isgrounded == false && nextHit < Time.time){
isgrounded = true;
if (distToGround > 65){ gethit(distToGround); sysmessage.snd("you are hurt from the fall. @3"); distToGround = 0;
if (animation.IsPlaying(("falling") || ("Horsefalling"))) { AnimationPlay("idle",1); }
}
}
isgrounded = true;
if (animation.IsPlaying(("falling"))) { AnimationPlay("idle",1); }
if (horsed == 1 && animation.IsPlaying(("Horsefalling"))) { AnimationPlay("horseman",1); }
}
*/
Your answer
![](https://koobas.hobune.stream/wayback/20220613142828im_/https://answers.unity.com/themes/thub/images/avi.jpg)