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 irvaz · Mar 09, 2014 at 05:39 PM · crouchgetbuttondowngetbutton

C# Problem returning from crouch to stand

I'm trying to make a simple crouch that when the crouch button is pressed the player crouches and stays crouched until the button is pressed again, where they return to standing. By just having my first if I can get my player to crouch with getButton but adding the second if to return them to crouching causes the crouch to become random and shaky. Does anyone have any ideas... Thanks

using UnityEngine;

 using System.Collections;
 
  
 
 [RequireComponent (typeof(CharacterController))]
 
 public class CrouchAndProne2 : MonoBehaviour 
 
 {
 
  
 
     CharacterController characterController;
 
     
 
     public bool isStanding = true;
 
     public bool isCrouching = false;
 
     
 
     float inHeight = 0.0f;
 
     Vector3 inCenter;
 
     
 
     public float standingHeight = 0.0f;
 
     public Vector3 standingCenter;
 
     
 
     public float crouchingHeight = 0.0f;
 
     public Vector3 crouchingCenter;
 
     
 
     public Transform mainCam;
 
     public Vector3 defCamHeight;
 
     public Vector3 curCamHeight;
 
     public Vector3 crouchCamHeight;
 
     float camSpeed = 20.0f;
 
     
 
     
 
     // Use this for initialization
 
     void Start () 
 
     {
 
         characterController = GetComponent<CharacterController>();
 
         
 
         isStanding = true;
 
         //isCrouching = false;
 
         
 
         inHeight = characterController.height;
 
         inCenter = characterController.center;
 
         
 
         standingHeight = inHeight;
 
         standingCenter = inCenter;
 
         
 
         crouchingHeight = inHeight - 0.451f;
 
         crouchingCenter = inCenter + Vector3.down * 0.2f;
 
     }
 
     
 
     
 
     void Awake()
 
     {
 
         defCamHeight = mainCam.localPosition;
 
     }
 
  
 
     
 
     // Update is called once per frame
 
     void Update () 
 
     {
 
         //Make camera follow crouch and prone.
 
         if(isStanding == true)
 
         {
 
             curCamHeight = defCamHeight;
 
         }
 
         if(isCrouching == true)
 
         {
 
             curCamHeight = crouchCamHeight;
 
         }
 
         
 
  
 
         //Lerp
 
         mainCam.localPosition = Vector3.Lerp(mainCam.localPosition, new Vector3(0.0f, curCamHeight.y, 0.0f), camSpeed * Time.deltaTime);
 
         
 
     
 
         
 
         //Make player go from standing to crouch.
 
         if(Input.GetButton("Crouch") == true && isStanding == true)
 
         {
 
             Crouch();
 
             
 
         }
 
         
 
         else if(Input.GetButton("Crouch") == true && isCrouching == true)
 
         {
 
             Stand ();
 
         }
 
         
 
     }
 
     
 
     
 
     void Stand ()
 
     {
 
         characterController.height = standingHeight;
 
         characterController.center = standingCenter;
 
         isStanding = true;
 
         isCrouching = false;
 
     }
 
     
 
     void Crouch ()
 
     {
 
         characterController.height = crouchingHeight;
 
         characterController.center = crouchingCenter;
 
         isCrouching = true;
 
         isStanding = false;
 
     }
 
     
 
  
 
     
 
 }
Comment
Add comment · Show 1
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 irvaz · Mar 10, 2014 at 07:01 PM 0
Share

Anyone else got any ideas?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by nesis · Mar 09, 2014 at 06:18 PM

 if (Input.GetKeyDown(KeyCode.E)) {
     if (isCrouching) {
         Stand();
     }
     else {
         Crouch();
     }
 }

Stand() and Crouch() should set isCrouching to false and true respectively, while also setting camera height, and the other things they do now.

Comment
Add comment · Show 3 · 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 irvaz · Mar 09, 2014 at 09:10 PM 0
Share

Thanks for the reply :) I've tried the above but I'm still getting the same problem. Pressing crouch still only works with some presses and when it does work its kind of shaky. I have my Crouch button set to the b button of an xbox controller if that could be causing a problem... I've checked and my input settings look fine. Also GetButtonDown didn't do anything so I'm using GetButton. Thanks.

avatar image nesis · Mar 15, 2014 at 03:24 PM 0
Share

using Input.GetButton() will toggle it on and off rapidly, on one frame, off the next. You need to use Input.GetButtonDown() or Input.Get$$anonymous$$eyDown() to detect the frame the player started pressing crouch, rather than every frame the user is holding crouch.

avatar image nesis · Mar 15, 2014 at 03:25 PM 0
Share

Also, make sure you use that in the Update() or LateUpdate() methods, since button state is updated once per frame.

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

21 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

Related Questions

Detecting if 2 Buttons are pressed at the same time 3 Answers

Won't Enter GetButton Statement 1 Answer

How can I use multiple GetButtonDown keys simultaneously? 2 Answers

More Accurracy on Mouse Click Detection. 0 Answers

Animation replaying every frame... 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