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
3
Question by Flipbee9 · Jul 08, 2010 at 06:26 PM · fpscharactercrouch

How To Make The FPS Character Crouch

I have put together a simple FPS game, but I just can't get my character to crouch. I've searched it up and have seen people shorten the Character Controller, but what I would like is to have the character controller shorten, and the camera as well. This way, the character can squeeze into places like vents, holes, or whatever. I've seen Controller.height = 0.5, but I also would like the camera to move down with it.

Thanks in advance :D

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

8 Replies

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

Answer by Jason_DB · Jul 08, 2010 at 06:42 PM

This is what I do, the function crouch lowers the character controller height & collider and sets crouching to true. crouchDeltaHeight is my variable for the height change when crouching. (All of this would be in the FPSWalker default script).

    function crouch() {
        this.GetComponent(BoxCollider).size -= Vector3(0,crouchDeltaHeight, 0);
        this.GetComponent(BoxCollider).center -= Vector3(0,crouchDeltaHeight/2, 0);
        crouching = true;
    }

And then in update the camera lowers to crouch height if crouching or returns to normal otherwise (it's in update to make it look smooth).

if(crouching){ if(mainCamera.transform.localPosition.y > crouchingCamHeight){ if(mainCamera.transform.localPosition.y - (crouchDeltaHeight*Time.deltaTime*8) < crouchingCamHeight){ mainCamera.transform.localPosition.y = crouchingCamHeight; } else { mainCamera.transform.localPosition.y -= crouchDeltaHeight*Time.deltaTime*8; } } } else { if(mainCamera.transform.localPosition.y < standardCamHeight){ if(mainCamera.transform.localPosition.y + (crouchDeltaHeight*Time.deltaTime*8) > standardCamHeight){ mainCamera.transform.localPosition.y = standardCamHeight; } else { mainCamera.transform.localPosition.y += crouchDeltaHeight*Time.deltaTime*8; } }

}

This is the code to stop crouching and return to normal:

        function stopCrouching(){
            crouching = false;
            this.GetComponent(BoxCollider).size += Vector3(0,crouchDeltaHeight, 0);
            this.GetComponent(BoxCollider).center += Vector3(0,crouchDeltaHeight/2, 0);
        }

And here's what I have in update to trigger it:

if (Input.GetButtonDown ("Crouch")){ if(crouching){ stopCrouching(); return; }

 if(!crouching)
     crouch();

}

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 Flipbee9 · Jul 08, 2010 at 06:43 PM 0
Share

Again, another brilliant Answer from DastardlyBanana :D Thanks very much

avatar image Flipbee9 · Jul 08, 2010 at 07:15 PM 0
Share

I keep getting errors about crouchDeltaHeight, standardCamHeight and etc. I'm guessing I'm supposed to set up the variables and so I did so. But about the BoxCollider, I'm using a character controller ins$$anonymous$$d of a box collider

avatar image Jason_DB · Jul 08, 2010 at 07:48 PM 0
Share

Well then you can probably just cut that out.

avatar image josh.du · Aug 12, 2010 at 08:51 PM 0
Share

how do i make this work

avatar image Rares15 · Dec 24, 2016 at 07:51 AM 0
Share

Can you put an code but in C#?

avatar image
0

Answer by Kevin Ambruster · Aug 20, 2010 at 06:46 PM

I don't know if this is the best solution but I was thinking as long as you had the camera parented to the capsule you are using as your character you could just make an animation that scales the capsule on the y-axis.

Comment
Add comment · Show 2 · 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 Jordan Miller 2 · Oct 06, 2010 at 06:36 PM 0
Share

you could do that but I've learned through sad experience that when you do that an you're holding a gun or other mesh it gets scrunched too.

avatar image Jordan Miller 2 · Oct 06, 2010 at 06:37 PM 0
Share

however that can be remedied by replacing the mesh every time you crouch or stand up.

avatar image
0

Answer by Donald Seburn · Apr 12, 2011 at 05:05 PM

My suggestion would be having five variables, for the hieght and camera position before and after crouching and a variable that ays if you are crouching. Have the camera position be relative, so that it is always added to the characters own position. If your weapons are a child of your camera then you will see them move down as well. This may be an issue with graphics, as if you are using a mesh for the character you will have to create an entire motion sequence seperate from the original and have them programmed individualy (Animation Programing can be a pain in FPS) and afterwards should you want a death scene the rigidbody formed would have to be altered for a 'crouch death'. Lastly you would be forced to decide how your crouching affects your jumping, whether you can jump when crouched, if so can you jump as high, and that would involve delving into the fps character made by unity (I am presuming you are using it.) which is a difficult process.

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 hunter21 · Jul 05, 2011 at 02:47 PM

try this... works perfectly :)

 var target : Transform;
 var initial :Transform;
 private var old : Transform;
 var bool : boolean;
 var smooth = 100.0;
 
 function Start(){
 
   initial.position=transform.position;
   }
 
 function Update () {
 if(Input.GetKey("c"))
 {  
    bool = true;
 }
 else
  bool=false;
 
 if(bool)
 {
   
   transform.position = Vector3.Lerp (transform.position,
                target.position,Time.deltaTime * smooth);
 }
 
 else 
 transform.position = Vector3.Lerp (transform.position,
              initial.position,Time.deltaTime * smooth);
 }
Comment
Add comment · Show 2 · 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 Chris D · Jul 05, 2011 at 03:59 PM 0
Share

If you're going to post code, please remember to format it correctly (101010 button).

avatar image shailesh I · Dec 14, 2014 at 11:48 AM 0
Share

Explain what exactly is Target, Intial and Old variable is

avatar image
0

Answer by fAvorable · Oct 01, 2011 at 09:10 PM

Fore anyone trying to use the FPS controller:

 private var walkSpeed: float = 8;    // regular speed
 
 private var runSpeed: float = 15;    // run speed
 
 private var crouchSpeed: float = 3;  // crouching speed
 
 private var speed: float;            // the current speed state
 
 
 
 private var crouchHeight : float;     // variables for character height while crouching
 
 private var crouchingHeight : float;  
 
 private var standingHeight: float;    // height of character while standing
 
 private var crouching = false;        // boolean is crouching?
 
 private var running = false;          // boolean is running?
 
 
 
 private var characterMotor: CharacterMotor;  //direct variable to the characterMotor
 
 private var mainCamera: GameObject;          // direct variable to the mainCamera
 
 
 
 function Start(){
 
     //initialize
 
     characterMotor = GetComponent(CharacterMotor);
 
        mainCamera = gameObject.FindWithTag ("MainCamera");
 
        standingHeight = mainCamera.transform.localPosition.y;
 
     crouchingHeight = (standingHeight / 2);
 
     crouchHeight = (standingHeight - crouchingHeight);
 
 }
 
 
 
 function Update(){
 
     
 
     //simple machine for setting the current speed of the character
 
     if( crouching ){
 
         speed = crouchSpeed;
 
     }else if( running ){
 
         speed = runSpeed;
 
     }else{
 
         speed = walkSpeed;
 
     }
 
     
 
     //If the sprint button is clicked, and not crouching .. SPRINT!
 
     if (Input.GetKey("left shift") || Input.GetKey("right shift")){
 
         if(!crouching){
 
             running = true;
 
             CameraZoom.sprint = true;    //sets sprint true so the camera knows to pan out
 
         }
 
     }else {
 
         CameraZoom.sprint = false;
 
         running = false;
 
     }
 
   
 
       // if crouch button has been pressed, put FPS into crouch
 
     if (Input.GetButtonDown ("Crouch")){
 
         if(crouching){
 
             stopCrouching();
 
             return;
 
             }
 
         if(!crouching)
 
                 crouch();
 
     }
 
   
 
     // crouch with smoothing
 
     if(crouching){
 
         if(mainCamera.transform.localPosition.y > crouchingHeight){
 
             if(mainCamera.transform.localPosition.y - (crouchHeight*Time.deltaTime*8) < crouchingHeight){
 
                 mainCamera.transform.localPosition.y = crouchingHeight;
 
             } else {
 
                 mainCamera.transform.localPosition.y -= crouchHeight*Time.deltaTime*8;
 
             }
 
         }
 
         } else {
 
         if(mainCamera.transform.localPosition.y < standingHeight){
 
             if(mainCamera.transform.localPosition.y + (crouchHeight*Time.deltaTime*8) > standingHeight){
 
                 mainCamera.transform.localPosition.y = standingHeight;
 
             } else {
 
                 mainCamera.transform.localPosition.y += crouchHeight*Time.deltaTime*8;
 
             }
 
             }
 
     }
 
     //set the max speed in the char motor;
 
      characterMotor.movement.maxForwardSpeed = speed;
 
 }
 
 
 
 // sets teh size of the collider to crouch size
 
 function crouch() {
 
         this.GetComponent(CharacterController).height -= crouchHeight;
 
         this.GetComponent(CharacterController).center -= Vector3(0,crouchHeight/2, 0);
 
         crouching = true;
 
  }
 
 // resets the size of the collider to standing size  
 
  function stopCrouching(){
 
         crouching = false;
 
         this.GetComponent(CharacterController).height += crouchHeight;
 
         this.GetComponent(CharacterController).center += Vector3(0,crouchHeight/2, 0);
 
 }  
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
  • 1
  • 2
  • ›

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FPS Character with model 6 Answers

force player to crouch when under an object with collisio 2 Answers

Make a mesh invisible but still cast shadows 1 Answer

Camera Height-based crouch script 2 Answers

Character jumping very high for no reason 3 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