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 Resdin · Mar 03, 2017 at 09:20 PM · c#scripting problemcharactercontrollerfalls

Raycast fall detection and issues with .isGrounded,

Good afternoon. First time asking a question here and first time working with the animator state machine. Thank you in advance.

1) Get to the edge of a rigid body / collider, the character will get stuck playing the falling animation and not fall or register an infinite fall; seems the characterController collider gets stuck no matter the skin width. Usually if the char is jogging this will not occur and he will fall and land perfectly, it usually happens while walking - particularly walking at angles less than / greater than 90 degrees (holding two keys? ex W,D). If I move the character in scene view he will fall but sometimes will still be stuck in the falling animation or it will not play the landing animation. I tried the relevant fixes I found on google but none worked.

2) While crouching and moving, my raycast detects the character as falling. Even the charactercontroller .isGrounded registers being ungrounded. You'll see in the code where I have stepped through the fall detection using the debug log. I have looked at the collider in play mode and everything looks fine, although the collider does not shrink to match the character which I would like to also resolve but that is for later.

Anyway to improve the accuracy / efficiency of this code? Should I do fall detection from a different script? Any advice would be greatly appreciated. It's currently in my movement script attached to the player rather than a separate fall detection script.

I've made two state machines, one which controls the character out of combat, and one while in combat -- "unlockedMachine" and "lockedMachine" respectively.

 void Start () { //I have also put this in Awake()
         var dist = 0f; 
         GetHitDistance (out dist); 
         initialDistance = dist; 
     }
         bool GetHitDistance (out float distance){ 
             distance = 0f;
             Ray downRay = new Ray (transform.position, -Vector3.up); //cast a downward ray
             if (Physics.Raycast (downRay, out hit)) { //if the ray collides
                 distance = hit.distance; //store the distance
                 return true; //return true, that the ray has collided
             }
             return false; // else return false
         }
     void Update () //I have put this into FixedUpdate, did not help.
     {
         Vector3 cameraForward = new Vector3 (cameraPivot.forward.x, 0.0F, cameraPivot.forward.z).normalized;
         Vector3 cameraRight = new Vector3 (cameraPivot.right.x, 0.0F, cameraPivot.right.z).normalized;
         Vector3 movement = new Vector3 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"), 0.0F);

         var dist = 0f;
         if (controller.isGrounded) {
             Debug.Log ("registering as grounded");
             /*UnlockedMachine*/
             if (anim.GetBool ("locking") == false) { //if you are not locked on
                 if (Input.GetKeyDown (KeyCode.LeftShift)) { //if you press left shift
                     if (anim.GetBool ("sprint") == true) { //if you are sprinting
                         anim.SetBool ("sprint", false); //turn sprint to false
                         anim.SetBool ("moving", false); //stop the character from sprinting
                     } else {                            //else
                         if (anim.GetBool ("run") == true) { //if the character is running
                             anim.SetBool ("sprint", true); //then the player should sprint
                             anim.SetBool ("run", false); //stop the character from running
                         } else {                        //else
                             if (anim.GetBool ("run") == false) { // if the character is not running
                                 anim.SetBool ("run", true); // make him run
                             }
                         }
                     }
                 }
                 if (anim.GetFloat ("DirectionY") == 0 && anim.GetFloat ("DirectionX") == 0 && anim.GetBool ("sprint") == true) {
                     anim.SetBool ("sprint", false); //if the char is not moving reset the sprint bool
                 }
 
                 if (anim.GetFloat ("DirectionY") == 0 && anim.GetFloat ("DirectionX") == 0 && anim.GetBool ("run") == true) {
                     anim.SetBool ("run", false); //if the char is not moving reset the run bool
                 }
                 if (Input.GetKeyDown (KeyCode.C)) { //if you press C
                     if (anim.GetBool ("crouch") == false) { //if you are not crouching
                         anim.SetBool ("crouch", true); //crouch
                     } else {                          //else
                         if (anim.GetBool ("crouch") == true) { //if you are crouching
                             anim.SetBool ("crouch", false); //turn crouch off
                         }
                     }
                 }
 
             } else { //you must be locked on
                 anim.SetBool ("locking", true); //locking true
                 anim.SetBool ("run", false); //cannot run
                 anim.SetBool ("sprint", false); //cannot sprint
             }
 
             /*LockedMachine*/
             if (anim.GetBool ("locking") == true) { //if you are locked on
                 rotationDamp = .2f; //reduce rotation damping to increase the speed of aiming between targets
                 if (Input.GetKeyDown (KeyCode.C)) { //if the player presses C
                     if (anim.GetBool ("crouch") == false) { //if the player is not crouching
                         anim.SetBool ("crouch", true); //make the player crouch
                     } else {                         //else
                         anim.SetBool ("prone", true); //make the player go prone
                         anim.SetBool ("crouch", false); //and turn off crouch
                     }
                 }
                 if (Input.GetKeyDown (KeyCode.X)) { //if the player presses X
                     if (anim.GetBool ("prone") == true) { //and prone is true
                         anim.SetBool ("crouch", true); //set crouch true
                         anim.SetBool ("prone", false); //set prone false
                     } else {                            //else
                         if (anim.GetBool ("crouch") == true) { //if crouch is true
                             anim.SetBool ("crouch", false); // turn crouch off
                         }
                     }
                 }
             } else {                         //else
                 anim.SetBool ("prone", false); //turn prone off
                 rotationDamp = .5f; //return rotation dampening to normal
             }
         } else {
             Debug.Log ("Registering a Fall");
             if (GetHitDistance (out dist)) { //if the raycast has hit something
                 Debug.Log ("Step1");
                 if (initialDistance < dist) { //if the distance is greater than 0
                     Debug.Log ("Step2");
                     var relDistance = dist - initialDistance;//deltatime? Get relitive distance Distance from ground - initial distance
                     if (relDistance > fallingThreshold) { //if the distance is greater than the fall sensitivity set above register a fall
                         Debug.Log ("Step3");
                         if (relDistance > maxFallingThreshold) { //will you take damage?
                             Debug.Log ("Step4");
                             //Fall dmg or sounds here
                             isFalling = true; //falling is true
                             anim.SetBool ("falling", true);//change animator state
                             anim.SetBool ("locking", false);//change animator state
                             anim.SetBool ("crouch", false);//change animator state
                             anim.SetBool ("prone", false);//change animator state
                             anim.SetBool ("run", false);//change animator state
                             anim.SetBool ("sprint", false);//change animator state
                             anim.SetBool ("landing", false);//change animator state
                         } else { 
                             Debug.Log ("Step5-Landing");
                             anim.SetBool ("landing", true);//very short fall, skip fall animation and play landing animation
                             anim.SetBool ("falling", false);//change animator state
                             isFalling = false; //flip the falling bool
                         }
                     }
                 }
             } else {
                 Debug.Log ("Infinite Fall");//else there is no ground
             }
         }
 //regular movement detection here as well as target locking removed to save space
 }

 void LateUpdate(){
     if (!isFalling) {
         anim.SetBool ("landing", false); // to reset the landing bool to false after landing
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Resdin · Mar 04, 2017 at 08:19 PM

After more testing, I have found the issue with the slow / unreliable transitions was due to improper state machine transition settings in the animator.

I still have the issue where the CharacterController gets stuck on edges instead of falling and that the raycast and .isGrounded register a fall while crouching and moving so any help directed at those issues would be very helpful. I will edit the main question to reflect that.

Comment
Add comment · Share
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

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

115 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 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

Character's jumping mechanism stuck into something invisible 1 Answer

Adding a CharacterController makes my player move around the map involuntarily 0 Answers

Help with OnTriggerEnter Not working correctly? 1 Answer

Character swap breaks movement. 1 Answer

How to unload an scene loaded in addictive mode ? 0 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