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 politdejan8 · Oct 21, 2019 at 06:00 AM · scripting problemplayer movementcrouchcrouching

Keep Player Crouching

Hi, I'm new on Unity and I'm trying to make my player crouch. I got it but when I put some objects to go under them I go stand up and my player gets stucked. The colliders overlaps. Anybody can help me? There's the code (the comments are on my native language, sorry):

This image should explain my problem better: https://imgur.com/a/dFHSLy1

  //Variables de acciones
     bool jump; //Comprueba si el personaje está saltando.
     bool sprint; //Comprueba si el personaje está corriendo.
     bool crouch; //Comprueba si el personaje está agachado.
 
     float originalHeight; //Altura original de personaje.
     float originalCharCollision;//Altura original de la colisión del personaje.
     float crouchHeight = 1.5F; //La altura que queremos que tenga nuestro personaje al agacharse.
     float crouchCharCollision = 1.5F; //La altura que queremos que tenga la colisi´ñon de nuestro personaje al agacharse.
 
 
 
     // Start is called before the first frame update
     void Start()
     {
         character = GetComponent<CharacterController>(); //Selecciona al personaje
         charColision = GetComponent<CapsuleCollider>(); //Selecciona la colisión
         originalHeight = character.height; //Coge la altura que le hayamos pasado en Unity al Character Controller
         originalCharCollision = charColision.height; // Coge la altura que le hayapmos pasado en Unity al Capsule Collider
 
 
     }
 
     // Update is called once per frame
     void Update()
     {
         //Variables para el movimiento
         moveHorizontal = Input.GetAxis("Horizontal");// GUardamos el movimiento horizontal (Derecha 1 | Izquierda -1)
         moveVertical = Input.GetAxis("Vertical"); //Guardamos el movimeinto vertical (Arriba 1 | Abajo -1)
 
         //Metodos
         Move();
         Crouch(); //Crouch es capaz de detectar por si sola si esta agachado o no gracias a CheckCrouch().
 
         if (!sprint)//Ejecuta el método Sprint() cuando el personaje no está corriendo.
         {
             Sprint();
         }
 
         if (!jump)//Ejecuta el método Jump() cuando el personaje no está saltando. Con esto conseguimos que no hayan saltos infinitos.
          {
             Jump();
          }
 
     }    
 void Crouch()
     {
         if (character.isGrounded)
         {
             if (Input.GetKey(KeyCode.LeftControl))
             {
                 crouch = true;
                 CheckCrouch();
             }
             else
             {
                 crouch = false;
                 CheckCrouch();
             }
         }
     }
 
     void CheckCrouch()
     {
         if (crouch)
         {
             character.height = crouchHeight; //Cambia la altura de nuestro personaje por la que le hemos pasado anteriormente
             charColision.height = crouchCharCollision; //Cambia la altura de colisión de nuestro personaje por la que le hemos pasado anteriormente
 
         }
         else
         {
             //Vuelve a su estado original
             character.height = originalHeight; 
             charColision.height = originalCharCollision;
         }
     }
 }
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

3 Replies

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

Answer by Ermiq · Nov 18, 2019 at 10:38 AM

Assuming you use CapsuleCollider on your character, you'll need to cast a virtual capsule identical to the character's capsule to check for any potential collisuons up there. Physics.CapsuleCast() is the function you can you in this case, but since you don't need to check any additional space above the character you could just use a simpler option - Physics.CapsuleCheck(). There are also Physics.SphereCast(), Physics.RayCast(), Physics CheckBox() etc. Which one to use depends on the situation.

Imagine the character's capsule as two spheres and a cylinder between the spheres.

   ___
  /   \  - upper sphere
  \___/
  |    |
  | __ |
  /    \
  \____/ - lower sphere

You will cast this capsule at the character's position to check if there are any collisions at the space where the character will stand. The capsule height should be set to the full character height as when the character is not crouching.
Also, you need to ignore the character himself while checking the collisions, otherwise, the CheckCapsule() will return true because it will detect the collision with the character. To ignore the character you need to set a layer for the character, in the character's inspector find Layer menu and create the layer Player and set the character to it.
Now in the code, a couple of bitwise operations:

 // create the int variable to store the player layer
 int playerLayer = -1;
 // and the variable which will represent all layers except the player's layer. It will be used in 'Physics.CheckCapsule()' to detect all collisions in all layer but to ignore the player layer.
 int allLayersExceptPlayer;

 //in Start() get the layer mask which will include all layers except the 'Player' layer
 void Start() {
     //Convert Layer Name to Layer Number
     playerLayer = LayerMask.NameToLayer("Player");
     // or you can just use number of the layer, e.g. your player layer is 1, then 'playerLayer = 1;'
     
     //Calculate layermask of the player
     allLayersExceptPlayer = (1 << playerLayer);
     //and invert to ignore it.
     allLayersExceptPlayer = ~allLayersExceptPlayer;
 }

 void Crouch()
  {
      if (character.isGrounded)
      {
          if (Input.GetKey(KeyCode.LeftControl))
          {
              crouch = true;
              CheckCrouch();
          }
          // set 'crouch=false' only when there's a space to stand straight
          else if (CanStandUp())
          {
              crouch = false;
              CheckCrouch();
          }
      }
  }

 // casts a capsule to check the space above the chatacter
 private bool CanStadUp() {
     if (Physics.CheckCapsule(
         //param1 - the center of the lower sphere, it's the character position point but higher.
         // the height is character's radius. Also, you probably will need to add some offset to prevent the collisions with the ground,  e.g. the physics default collision detection offset
         transform.position + Vector3.up * (characterRadius + Physics.defaultcontactoffset),
         // param2 - the center of the upper sphere
         // the character's full height minus his radius
         transform.position + Vector3.up * (characterFullHeight - characterRadius),
         // param3 - the radius of the spheres, but a bit less to prevent the sphere collision with the ground
         characterRadius - Physics.defaultcontactoffset,
         // param4 - layers to check, use our layer mask qhich we've got in the 'Start()'
         allLayersExceptPlayer,
         QuerryTriggerInteraction.Ignore)
     // the result of the 'CheckCapsule' is true when some collisions were detected in the given capsule space within the given layers. Return 'false' i.e. can't stand now.
        return false;
     else
     // if there's nothing was detected (even the character himself wasn't detected because the function has ignored his layer)
         return true; // can  stand up
 }
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
avatar image
0

Answer by lgarczyn · Oct 21, 2019 at 06:16 PM

Simply do a CapsuleCast upwards to check if the player is allowed to stop crouching.

Comment
Add comment · Show 4 · 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 politdejan8 · Oct 22, 2019 at 07:48 AM 0
Share

How can I do that? I'm learning Unity and don't know so much about program$$anonymous$$g yet

avatar image Favouriteless politdejan8 · Oct 25, 2019 at 12:00 PM 1
Share

@politdejan8 https://docs.unity3d.com/ScriptReference/Physics.CapsuleCast.html

The best thing to $$anonymous$$ch you is to find out yourself. Everything you will ever need to know about Unity is in the documentation (other than very niche things)

avatar image lgarczyn politdejan8 · Oct 25, 2019 at 11:14 PM 0
Share

CapsuleCast is a function that basically tries to move a CapsuleCollider through the world from one place to another, and gives you the distance to the first thing it hits

avatar image JohannaA · Nov 18, 2019 at 08:58 AM 0
Share

I've spent hours and hours trying to get this to work as a straight up beginner. I've copied the code exactly. 2019.1.5f1 version. I was able to troubleshoot that somewhere the code is causing Crouch to be always on LiteBlue

avatar image
0

Answer by AppDosis · Nov 21, 2019 at 10:39 PM

If you want to learn a lot about crouching and character controllers i recommend you check "Kinematic Character Controller" on the asset store, it helped me a lot!

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

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

My Player is going through the floor after crouching 0 Answers

Lock player movement within screen bounds 2 Answers

crouch script problem 1 Answer

W key to move doesnt work,Movement Script doesnt do anything when i press w 0 Answers

Character not moving towards desired random location 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