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 Miaw666 · Dec 28, 2020 at 07:13 PM · animationcrouching

Crouch animation code needs to be shorter?

Hey there! I have tried to write code for crouching for a long time and write or find one that works as well as I want it to. I gave up and decided to use animations ("Crouch Up" and "Crouch Down"). I wrote some code that works almost perfectly but the problem is that it's too long; I was wondering if someone could help me make it shorter (I like things to be as close to perfect as possible, since perfect is not possible).

Additional problem: [When I'm moving while crouching, if I crouch up a little before I reach for example a table, since the crouching up takes a little time, the player will crouch up into the top of the table. I fixed it (you can see it in the code) but since it's an animation it's kinda buggy; The player starts the "crouch up" animation while canCrouchUp is true, canCrouchUp turns false because the player is currently under something, the "crouch up" animation continues playing, then instantly the "crouch down" animation starts playing because based on the code, the player should be crouching.]

     public bool canCrouchUp;
     void Crouching()
     {
         if (Input.GetButtonDown("Crouch") && canCrouchUp)
         {
             isCrouched = !isCrouched;
         }
         if (!isCrouched)
         {
             movementSpeed = walkingSpeed;
             if (canCrouchUp)
             {
                 GetComponent<Animator>().Play("Crouch Up");
             }
             else
             {
                 isCrouched = true;
             }
         }
         else
         {
             GetComponent<Animator>().Play("Crouch Down");
             movementSpeed = crouchSpeed;
         }
         if (isCrouched && !canCrouchUp)
         {
             GameObject.Find("cantStandText").GetComponent<Text>().text = "Can not stand up here.";
         }
         else { GameObject.Find("cantStandText").GetComponent<Text>().text = null; }
     }

In Update I have:

         canCrouchUp = !Physics.CheckCapsule(transform.position + new Vector3(0f, -0.325f, 0f), transform.position + new Vector3(0f, 0.675f, 0f), 0.25f, crouchingLayer); //CheckCapsule to not crouch up into something.
 

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
1

Answer by sacredgeometry · Dec 29, 2020 at 05:14 PM

You should think about your code a little more before you sit down to write it and shortness isnt a good metric to judge good code. Legibility is a better one:


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Tester : MonoBehaviour
 {
     public bool IsCrouched;
     public bool CanStand; // NOTE: I dont know where this is set
     private Text CantStandWarning;
 
     void Start()
     {
         CantStandWarning = GameObject.Find("cantStandText").GetComponent<Text>();
     }
 
     void Update()
     {
         if(Input.GetButtonDown("Crouch"))
             HandleCrouch();
     }
 
     void HandleCrouch()
     {
         if(IsCrouched)
         {
             if(CanStand)
             {
                 CantStandWarning.text = null;
                 IsCrouched = false;
                 GetComponent<Animator>().Play("Crouch Up");
                 movementSpeed = walkingSpeed;
             }
             else if(string.IsNullOrWhitespace(CantStandWarning.text))
             {
                 CantStandWarning.text = "Can not stand up here.";
             }
         } 
         else
         {
             IsCrouched = true;
             GetComponent<Animator>().Play("Crouch Down");
             movementSpeed = crouchSpeed;
         }
     }
 }
 
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

268 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Can the animation editor create local rotational data? 3 Answers

Adding animation clips via script 2 Answers

Can I make animations snap to a frame? 1 Answer

Play Animation 2 Answers

How to select an animation clip by index number? 7 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