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 /
avatar image
0
Question by Cobradabest · Jan 28, 2013 at 09:44 PM · c#colliderinputprogramming

Problems with crouching (FPS/C#)

I'm having a bit of a problem, I'm trying to add a crouch function to my game, I have the animation and camera position working perfectly, but the problem is the collider.

I'm trying to reduce the collider size and position, but when I do that, every time I go to crouch, then back up, I always go right through the floor.

Here's my code:

     private bool crouching;
     public float crouchColliderHeight;
     public float crouchColliderYPosition;
     private float currentColliderHeight;
     private float currentColliderYPosition;
     private CharacterController characterCollision;    
 void Start () 
     {            
 currentColliderHeight = characterCollision.height;
             currentColliderYPosition = characterCollision.center.y;
     }
 void Update () 
     {
             crouching = Input.GetButton("Crouch");
             if (crouching)
             {
                 characterCollision.height = crouchColliderHeight;
                 characterCollision.center = new Vector3(characterCollision.center.x, crouchColliderYPosition, characterCollision.center.z);
             }
             else
             {
                 if (characterCollision.height != currentColliderHeight)
                 {
                     float previousGravity = gravity;
                     characterCollision.height = currentColliderHeight;
                     gravity = 0;
                     characterCollision.center = new Vector3(characterCollision.center.x, currentColliderYPosition, characterCollision.center.z);
                     gravity = previousGravity;
                 }
             }
 }

What am I doing wrong?

Also, is there anyway to make it so that if the crouch collider is hitting something above it, it will stay crouching even after you let go of the crouch key?

Comment
Add comment · Show 2
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 Setzer22 · Jan 28, 2013 at 10:11 PM 0
Share

Unfortunately I can't tell what your first problem is about. But I got an approach to your second problem. You might get this to work if you add another collider to your character asset. You can make this collider a box or capsule collider, and make it the same size as the character controller, but make it trigger ins$$anonymous$$d of a normal collider. Then, when crouched, you'll be able to check if that trigger is actually colliding with something, that would tell you that your character isn't able to stand up at that point, so he should stay crouched

avatar image Cobradabest · Jan 28, 2013 at 10:14 PM 0
Share

Once I get the first problem fixed, I could definetly give that solution a try, it might just work, thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Gizmoi · Jan 28, 2013 at 10:23 PM

Edit: Moved comment to answer:

I think what's happening is that you're shrinking the collider, thus the player drops to a crouch height, then when you expand the collider again you are intersecting the floor, so physics just assumes you've gone too far and drops you. Try translating the player up by the change in collider height.

Also, for your second problem, probably best to make use of triggers in a different way. Decide on the areas where you must remain crouched and place a box collider around that area, then tell your player upon entering and remaining in that collider he must be crouched. This way he will not attempt to stand for a frame, then hit a trigger and find out he must then crouch again, he will simply stay crouched.

Comment
Add comment · Show 5 · 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
avatar image Cobradabest · Jan 28, 2013 at 10:32 PM 0
Share

I've tried the first solution, as you can see in the code, but the player seems to go through the floor regardless. Unless I'm doing it wrong?

avatar image Gizmoi · Jan 28, 2013 at 10:37 PM 0
Share

Have you had a look in the scene view with the collider selected so that you can see what happens to it as you crouch and un-crouch?

Where are you setting crouchYPosition and crouchColliderHeight?

Also I wouldn't bother with the gravity, you're essentially disabling and enabling it in the same frame, which should have no effect.

avatar image Cobradabest · Jan 28, 2013 at 10:44 PM 0
Share

I've had a look as as far as I can see, the position of the bottom both colliders are the same, they're just different heights.

I'm setting the crouch height and y position using the inspector, because the game I'm making will have multiple characters, and they're all different heights.

avatar image Cobradabest · Jan 28, 2013 at 10:51 PM 0
Share

Oh! Hold on! I just solved it! All I did was set the crouchcolliderYPosition down a bit, so it wouldn't go through the floor, and it worked!

avatar image gpscisco · Mar 28 at 09:02 AM 0
Share

This post helped solve my similar problem. I include a timer because the game is for dodging obstacles by jumping/crouching, the player will automatically stand back up after a set time.

My code:

 public class PlayerController : MonoBehaviour
 {
     private BoxCollider playerBC;
     private float crouchTime = 1f;
     private float timer;
     private Vector3 boxInitCenter;
     private Vector3 boxInitSize;
 
 void Start()
     {
         playerBC = GetComponent<BoxCollider>();
         boxInitCenter = playerBC.center;
         boxInitSize = playerBC.size;
     }
 
 void Update()
     {
         
         //for crouching
         if (Input.GetKeyDown(KeyCode.LeftControl) && isGrounded && !gameOver)
         {
             timer = Time.time;
             playerBC.size = new Vector3 (playerBC.size.x, 2.23f, playerBC.size.z);
             playerBC.center = new Vector3 (playerBC.center.x, 1.24f, playerBC.center.z);
         }
         //for standing after crouching
         if (Time.time > timer + crouchTime)
         {
             playerBC.size = boxInitSize;
             playerBC.center = boxInitCenter;
         }
     }

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Relationship between Events and Update() 2 Answers

Instantiating Prefabs through Editor Script 1 Answer

Character cannot move Diagonally forwards 0 Answers

Trouble with Iterative Coroutine 1 Answer


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