falling script flaws
found a script to detect player falling but there are two problems maybe someone can help with 1. the ray casts from the very bottom of the player - the capsule collider I imagine - and no matter what I do to the parameters of that capsule collider a little bit is always just a tad below the ground so the ray cast is reading below the ground to start with. So mesh colliders are out. I fix this by using a thicker box collider for the ground so that isn't the real problem. the real problem comes when I want to jump from one platform to another. The script works fine if the jump is a fail and I miss the other platform and...fall. BUT, even if the jump is going to be successful, midway across the gap the raycast hits the ground far below and thinks I am falling - thus triggering my fall animation. ANy ideas on how to fix this?. the script is suppose to detect if I am falling thus triggering my "falling" looped animation, playing a scream audio file and also enabling a box collider trigger zone on the ground so when I land - that trigger zone "crashland" takes away all my health as well as triggering an animation of the player crumpling in a heap of broken bones. 2. My work around for the problem of the raycast hitting the ground below mid jump is to have a box collider in that gap to block the raycast from hitting the ground. this box collider is a child of another collider which is a trigger that if i hit it while jumping ( and failing) the trigger gameobject is destroyed - along with the child collider I had blocking the raycast so the ray will then hit the ground and do the whole fallTo My Death bit. But I don't think that is the best solution by any means.
using UnityEngine;
public class newFallCode : MonoBehaviour
{
public float fallingThreshold = 1f;
public float maxFallingThreshold = 20f;
private float initialDistance = 3f;
private RaycastHit hit;
private Animator anim;
private BoxCollider boxCollider;
GameObject crashland;
AudioSource scream;
void Start()
{
crashland = GameObject.Find ("crashland");
boxCollider = GameObject.Find ("crashland").GetComponent<BoxCollider>();
var dist = 0f;
GetHitDistance(out dist);
initialDistance = dist;
anim = GetComponent <Animator>();
scream = GameObject.Find ("crashland").GetComponent<AudioSource>();
}
bool GetHitDistance(out float distance)
{
distance = 0f;
Ray downRay = new Ray(transform.position, -Vector3.up); // this is the downward ray
if (Physics.Raycast(downRay, out hit))
{
distance = hit.distance;
return true;
}
return false;
}
void Update()
{
var dist = 0f;
if (GetHitDistance(out dist))
{
if (initialDistance < dist)
{
//Get relative distance
var relDistance = dist - initialDistance;
//Are we actually falling?
if (relDistance > fallingThreshold)
{
//How far are we falling
if (relDistance > maxFallingThreshold)
//Debug.Log("Fell off a cliff");
anim.SetTrigger ("Falling");
//How far are we falling
if (relDistance > maxFallingThreshold)
scream.enabled = true;
if (relDistance > maxFallingThreshold)
boxCollider.enabled = true;
}
}
}
else
{
//anim.SetTrigger ("Falling");
Debug.Log("Infinite Fall");
}
}
}
Your answer
Follow this Question
Related Questions
Player Movement Goes Crazy When Hit On Side Of Moving Platform 0 Answers
Using Raycast hit to set vector position then moving player to that position 2 Answers
Rigidbody falling not according to physics, only when script is applied. 0 Answers
GameObject disappears on Animation transition 0 Answers
How to prevent the player to move beyond certain x-position value 3 Answers