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 /
  • Help Room /
avatar image
0
Question by K_Tec · Jul 22, 2017 at 07:03 PM · scripting problemif-statementsif else

Why is this fi statement not working??

Hello,

this is my Player script and i added a Crouch function. If i press C it crouch down but if i press C again it will not stand up! What did i wrong?

public int playerID = 0; [Header("General")] public CharacterController cc; public bool canControl = true; public GameObject cam;

 [Header("Movement")]
 public float walkSpeed;
 public float runSpeed;
 public float crouchSpeed;
 public float gravity;
 public float jumpForce;
 public float crouchHeight;


   


 //private vars
 private int movementState = 0; // 0 standing , 1 walking , 2 running , 3 crouching , 4 vehicle , 5 ladder , 6 injured , 7 air
 private Vector3 movement = Vector3.zero;
 private bool canRun;
 private bool canJump;
 private float height;



 void Start()
 {
     height = cc.height;

 }


 void Update()
 {
     
  
     if (cc.isGrounded && canControl)
     {
         movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         movement = transform.TransformDirection(movement);
         movement *= walkSpeed;

         if (Input.GetButtonDown("Jump"))
         {
             movement.y = jumpForce;
         }

         if (Input.GetButton("Sprint"))
         {
             movement *= runSpeed;
         }

         if (Input.GetButtonDown("Crouch"))
         {
             Crouch(crouchHeight,crouchSpeed);
         }
     }

     else
     {
         movementState = 7;
     }

     movement.y -= gravity * Time.deltaTime;
     cc.Move(movement * Time.deltaTime);

 }




 void Crouch(float height, float crouchSpeed)
 {
     if(movementState != 3) // if we are not crouching
     {

         cc.height = Mathf.Lerp(height, crouchHeight, 3f);
         movementState = 3;

     }

     else   //if we are crouching stand up
     {

         cc.height = Mathf.Lerp(cc.height, height, 3f);
         movementState = 0;

     }

 }

Thanks in advance and Best Regards,

T

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vollmondum · Jul 22, 2017 at 07:19 PM

 if(movementState != 3) // if we are not crouching
      {
          cc.height = Mathf.Lerp(height, crouchHeight, 3f);
          movementState = 3;
          return; // if you don't exit the method, it changes your movementState to 3 and the next frame is executed. So basically you're standing up and crouching back again :)
      }
      else   //if we are crouching stand up
      {
          cc.height = Mathf.Lerp(cc.height, height, 3f);
          movementState = 0;
      }
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 K_Tec · Jul 22, 2017 at 07:30 PM 0
Share

Thanks to your answer! But It is still now working :(( THe player is not standing up!!

Please i need to fix that :((

Regards, T

avatar image Vollmondum K_Tec · Jul 22, 2017 at 08:06 PM 0
Share

Well, Lerp only works in Update

avatar image NoseKills Vollmondum · Jul 23, 2017 at 11:08 AM 1
Share

That's not true. Lerp is just a mathematical function that returns a value based on 3 other values and it does not matter where you call it.

The way the OP is using Lerp doesn't make much sense but it does cause a change in height if the 1st and 2 nd parameters are correct.

Because he uses a third parameter greater than 1, Lerp always returns the 2nd parameter, so he could as well replace Lerp with just assigning the second parameter and things would work the same way.

      if(movementState != 3)
      {
          cc.height = crouchHeight;
          movementState = 3;
      }
      else 
      {
          cc.height = height;
          movementState = 0;
      }
Show more comments

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

111 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

Related Questions

How to put an if statement inside an if statement? 3 Answers

If/else statement is always else 1 Answer

If statement not working. 0 Answers

Three button combination to open next scene 2 Answers

Linking Trigger to Input (or directly into Character Control script) 0 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