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 /
avatar image
0
Question by Rukas90 · May 27, 2018 at 10:14 AM · playerbugissuecrouchfalling-through-floor

Smooth Crouch Movement Issue

Hello everyone,

I made my character crouch in the game, but I am having this one annoying issue that I don't know how to solve. Whenever the player stands up from the crouch position it has this problem where the player is trying to fall through the ground but then because of the 'StickToGroundForce' it goes right back on the ground again. I'm using the standard Unity 'FirstPersonController'. It looks really buggy and I can't seem to solve it for some reason. Another thing that I found weird is the fact that originally the script was written in Javascript and it works without any problems, but for some reason when I rewritten the script to C# this problem appeared. Even though the script is pretty much the same thing..

If any one could give me some advice on this weird problem I would really appreciate it.

C# Script:

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.Characters.FirstPerson;
 
 public class Crouch : MonoBehaviour 
 {
     private CharacterController charController;
     private float charHeight; //Initial height
 
     private GameObject player;
 
     private Vector3 theTransform;
 
     public bool inVent;
     public bool inProne;
     public bool inCrouch;
 
     private FirstPersonController controller;
 
     void Start () 
     {
         theTransform = transform.position;
         player = this.gameObject;
         charController = GetComponent<CharacterController>();
         charHeight = charController.height;
         controller = player.GetComponent<FirstPersonController>();
     }
 
     void FixedUpdate () 
     {
         CrouchFunction();
         ProneFunction();
     }
 
     void CrouchFunction ()
     {
         float h = charHeight;
         float lastHeight = charController.height;
 
         if (Input.GetButton("Crouch") && charController.isGrounded && !controller.m_Jump && 
             !controller.m_onLadder && charController.isGrounded && !inProne)
         {
             h = charHeight * 0.4f;
 
             charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime);
             theTransform.y += (charController.height-lastHeight)/2;
 
             inCrouch = true;
         }
 
         if (Input.GetButton("Crouch") == false && charController.isGrounded && !inProne)
         {
             if(!inVent)
             {
                 charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime);
                 theTransform.y += (charController.height-lastHeight) / 2;
 
                 inCrouch = false;
             }
             else if(inVent)
             {
                 h = charHeight * 0.4f;
                 charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime);
                 theTransform.y += (charController.height-lastHeight)/2;
                 inCrouch = true;
             }
         }
 
         if(controller.m_onLadder || !charController.isGrounded)
         {
             charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime);
             theTransform.y += (charController.height-lastHeight)/2; //Fix vertical position
             inCrouch = false;
         }
     }
 
     void ProneFunction ()
     {
         float h = charHeight;
         float lastHeight = charController.height;
 
         if (Input.GetButtonDown("Prone") && !inProne)
         {
             inProne = true;
         }
 
         else if (Input.GetButtonDown("Prone") && inProne)
         {
             inProne = false;
         }
 
         if(inProne && charController.isGrounded)
         {
             h = charHeight * 0.2f;
             charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime);
             theTransform.y += (charController.height - lastHeight) / 2; 
         }
     }
 }

 



Javascript Script:

 #pragma strict
 
 private var charController : CharacterController;
 private var theTransform : Transform;
 private var charHeight : float; //Initial height
 
 private var player : GameObject;
 
 private var defaultWalkSpeed : float;
 private var defaultRunSpeed : float;
 
 public var inVent : boolean;
 public var inProne : boolean;
 public var inCrouch : boolean;
 public var inWater : boolean;
 
 private var controller : UnityStandardAssets.Characters.FirstPerson.FirstPersonController;
 
 function Start () 
 {
     theTransform = transform;
     player = this.gameObject;
     charController = GetComponent(CharacterController);
     charHeight = charController.height;
     controller = player.GetComponent(UnityStandardAssets.Characters.FirstPerson.FirstPersonController);
     defaultWalkSpeed = player.GetComponent(UnityStandardAssets.Characters.FirstPerson.FirstPersonController).m_WalkSpeed;
     defaultRunSpeed = player.GetComponent(UnityStandardAssets.Characters.FirstPerson.FirstPersonController).m_RunSpeed;
 }
 
 function Update () 
 {
     CrouchFunction();
     ProneFunction();
 }
 
 function CrouchFunction ()
 {
     var h = charHeight;
     var lastHeight = charController.height;
     
     if (Input.GetButton("Crouch") && !inWater && charController.isGrounded && !controller.m_Jump && !controller.m_onLadder && charController.isGrounded && !inProne)
     {
         h = charHeight*0.4;
 
         charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime);
         theTransform.position.y += (charController.height-lastHeight)/2;
 
         inCrouch = true;
     }
 
     if (Input.GetButton("Crouch") == false && !inWater && charController.isGrounded && !inProne)
     {
         if(!inVent)
         {
             charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime);
             theTransform.position.y += (charController.height-lastHeight)/2;
 
             inCrouch = false;
         }
         else if(inVent)
         {
             h = charHeight*0.4;
             charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime);
             theTransform.position.y += (charController.height-lastHeight)/2;
             inCrouch = true;
         }
     }
 
     if(controller.m_onLadder || !charController.isGrounded || inWater || !charController.isGrounded)
     {
         charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime);
         theTransform.position.y += (charController.height-lastHeight)/2; //Fix vertical position
         inCrouch = false;
     }
 }
 
 function ProneFunction ()
 {
     var h = charHeight;
     var lastHeight = charController.height;
 
     if (Input.GetButtonDown("Prone") && !inProne)
     {
         inProne = true;
     }
 
     else if (Input.GetButtonDown("Prone") && inProne)
     {
         inProne = false;
     }
 
     if(inProne && !inWater && charController.isGrounded)
     {
         h = charHeight*0.2;
         charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime);
         theTransform.position.y += (charController.height-lastHeight) / 2; 
     }
 }
 
 function OnTriggerEnter (col : Collider)
 {
     if(col.tag == "Water")
     {
         inWater = true;
     }
 }
 
 function OnTriggerExit (col : Collider)
 {
     if(col.tag == "Water")
     {
         inWater = false;
     }
 }

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

103 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

Related Questions

Crouch jump bug. 1 Answer

Player movement 0 Answers

Android 6.0 Marshmallow creates black bar along bottom of screen. 4 Answers

My player is falling through my second level 0 Answers

Problem with Unity 5.5.0f3 . Need help 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