Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jun 25, 2014 at 07:08 AM by Reefer for the following reason:

Other

avatar image
0
Question by Reefer · Jun 23, 2014 at 09:25 AM · javascriptmovementfallingcrouch

Player keeps going through the floor when crouching.

Hi all, I got an next problem with JS script: Why my player keeps going through the floor if I crouch? Can't understand what I'm doing wrong or how to prevent this from happening, floor has mesh collider and my player is the basic FPS Controller with character controller and rigidbody.

Here's the movement.js script which includes the code for crouching.

 var mainCam : GameObject;
 var animObject : GameObject;
 var fallAnimGO : GameObject;
 var moveSpeed : int = 2;
 var crouchSpeed : int = 1;
 var runSpeed : int = 5;
 var jumpSpeed : int = 10;
 var gravity : int = 20;
 var maxEnergy : float = 10;
 var energyRegenRate : float = 1;
 var Smooth : float = 5;
 
 var grounded : boolean = false;
 var running : boolean = false;
 
 var energy : float;
 private var originalSpeed : int;
 private var hitCeiling : boolean = false;
 public var moveDirection = Vector3.zero;
 private var controller : CharacterController;
 controller = GetComponent(CharacterController);
 
 private var IntialPosition : Vector3;
 private var InitialHeight : float;
 private var CrouchingHeight : float;
 private var InitialCenter : Vector3;
 private var CrouchingCenter : Vector3;
 private var defCamPos : float = 0;
 private var bobSpeed : float;
 private var bobAmount : float;
 private var timer = 0.0;
 private var camPos : float;
 private var crouching = false;
 
 function Start()
 {
     originalSpeed = moveSpeed;
     energy = maxEnergy;
     defCamPos = mainCam.transform.localPosition.y;
     camPos = defCamPos;
     InitialHeight = controller.height;
     CrouchingHeight = InitialHeight - .5f;
     InitialCenter = controller.center;
     CrouchingCenter = InitialCenter + Vector3.down * 0.1f;
     isCrouching = false;
 }
 
 function Awake()
 {
     animObject.animation.wrapMode = WrapMode.Loop;
 }
 
 function Update()
 {
     if(controller.isGrounded)
     {
         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= moveSpeed;
         if(Input.GetKeyDown(KeyCode.Space))
         {
             Jump();
         }
         if(controller.velocity.magnitude > (moveSpeed - 1) && controller.velocity.magnitude < (runSpeed - 1))
         {
             animObject.animation.CrossFade("Walk");
             bobSpeed = 0.25;
             bobAmount = 0.005;
         }
         else if(controller.velocity.magnitude > (runSpeed - 1))
         {
             animObject.animation.CrossFade("Run");
             bobSpeed = 0.29;
             bobAmount = 0.01;
         }
         else
         {
             animObject.animation.CrossFade("Idle");
             bobSpeed = 0;
             bobAmount = 0;
         }
         if(Input.GetKey(KeyCode.LeftShift) && (Input.GetAxis("Horizontal") || Input.GetAxis("Vertical")))
         {
         
             if(energy > 0)
             {
                 if(!crouching)
                 {
                     Run();
                 }
                 else
                 {
                     moveSpeed = crouchSpeed;
 
                 }
             }
             else
             {
                 running = false;
                 if(!crouching)
                 {
                     moveSpeed = originalSpeed;
                 }
                 else
                 {
                     moveSpeed = crouchSpeed;
                 }
             }
         }
         else
         {
             running = false;
             if(energy < maxEnergy)
             {
                 energy += energyRegenRate * Time.deltaTime;
             }
             if(!crouching)
             {
                 moveSpeed = originalSpeed;
             }
             else
             {
                 moveSpeed = crouchSpeed;
             }
         }
         
         var camHeight : Vector3 = new Vector3(mainCam.transform.localPosition.x, camPos, mainCam.transform.localPosition.z);
         var smoothHeight  = Vector3.Lerp(mainCam.transform.localPosition, camHeight, Time.deltaTime * Smooth);
         mainCam.transform.localPosition = smoothHeight;
         
         if(Input.GetKeyDown("c"))
         {
             if(!crouching)
             {
                 controller.height = CrouchingHeight;
                 controller.center = CrouchingCenter;
                 camPos = defCamPos - .5f;
                 crouching = true;
             }
             else
             {
                 controller.height = InitialHeight;
                 controller.center = InitialCenter;
                 camPos = defCamPos;
                 crouching = false;
             }
         }
         waveslice = 0.0; 
         horizontal = Input.GetAxis("Horizontal"); 
         vertical = Input.GetAxis("Vertical"); 
         if(Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0) { 
               timer = 0.0; 
         } 
         else
         { 
                waveslice = Mathf.Sin(timer); 
                timer = timer + bobSpeed; 
                if(timer > Mathf.PI * 2)
                { 
                   timer = timer - (Mathf.PI * 2); 
                } 
         } 
         if(waveslice != 0)
         { 
                translateChange = waveslice * bobAmount; 
                totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical); 
                totalAxes = Mathf.Clamp (totalAxes, 0.0, 1.0); 
                translateChange = totalAxes * translateChange; 
                mainCam.transform.localPosition.y = smoothHeight.y + translateChange; 
         } 
         else
         { 
            mainCam.transform.localPosition.y = smoothHeight.y; 
         } 
     }
     else
     {
         animObject.animation.CrossFade("Idle");
         bobSpeed = 0;
         bobAmount = 0;
     }
     moveDirection.y -= gravity * Time.deltaTime;
     if(hitCeiling)
     {
         moveDirection.y -= gravity * Time.deltaTime;
     }
     var flags = controller.Move(moveDirection * Time.deltaTime);
     hitCeiling = ((flags & CollisionFlags.CollidedAbove) != 0);
     if(energy > maxEnergy)
     {
         energy = maxEnergy;
     }
 }
 
 function Run()
 {
     moveSpeed = runSpeed;
     energy -= energyRegenRate * Time.deltaTime;
     running = true;
 }
 
 function Jump()
 {
     moveDirection.y = jumpSpeed;
     fallAnimGO.animation.CrossFadeQueued("Jump", 0.1, QueueMode.PlayNow);
 }
 
 function OnControllerColliderHit(Hit : ControllerColliderHit)
 {
     if(!controller.isGrounded && controller.velocity.y < -6)
     {
         fallAnimGO.animation.CrossFadeQueued("Fall", 0.3, QueueMode.PlayNow);
     }
 }

Comment
Add comment · Show 4
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
avatar image meat5000 ♦ · Jun 23, 2014 at 09:50 AM 0
Share

Click your character controller and observe the collider when you are crouching.

avatar image Reefer · Jun 23, 2014 at 10:08 AM 0
Share

So now I know it's happening when character should come back up, in other words, when player presses c it goes on crouch, after player presses c again, it drops through the floor as it comes to standing mode again, but still don't know how to fix this?

avatar image Reefer · Jun 23, 2014 at 11:55 AM 0
Share

As I found an fix for this.

It was the rigidbody on fps controller that caused this to happen, I took it off and now it works as it should. But why it doesnt work with rigidbody on? As now I can't anymore push objects.. :(

avatar image meat5000 ♦ · Jun 23, 2014 at 12:04 PM 0
Share

CC and rigidbody don't work very well together.

There are ways to make it work though, like using Root $$anonymous$$otion animation with a $$anonymous$$inematic rigidbody works with a CC I believe.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Basic 2d movement problem: glitching in terrain 0 Answers

WoW Movement and Freeze Cursor Location? 1 Answer

Player movement 0 Answers

Best Way to make a character move 1 Answer

How to make a car not go off the screen? 2 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