Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by shadowpuppet · Aug 05, 2016 at 07:17 PM · player movementraycasthitplatformfalling

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.alt text

 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");
         }
     }
 }


fallzone.jpg (119.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

64 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges