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 toroberg · Jul 23, 2019 at 05:40 PM · if-statementsif-else

Any way to slim down if/else statements?

Hi!

Anyone know a way to slim this code down a bit?

Code example below:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AnimationManagement : MonoBehaviour
 {
     Animator _animator;
     bool _runForward;
     bool _runBackward;
     bool _jump;
     bool _turnRight;
     bool _strafeRight;
     bool _turnLeft;
     bool _strafeLeft;
 
     void Update()
     {
         _animator = gameObject.GetComponent<Animator>();
         _runForward = false;
         _runBackward = false;
         _jump = false;
         _turnRight = false;
         _strafeRight = false;
         _turnLeft = false;
         _strafeLeft = false;
     }
 
     void Update()
     {
         // Run Forward
         if (Input.GetKey(KeyCode.W))
         {
             _runForward = true;
             _animator.SetBool("RunForward", true);
         }
         else
         {
             _runForward = false;
             _animator.SetBool("RunForward", false);
         }
 
         // Run Backward
         if (Input.GetKey(KeyCode.S))
         {
             _runBackward = true;
             _animator.SetBool("RunBackward", true);
         }
         else
         {
             _runBackward = false;
             _animator.SetBool("RunBackward", false);
         }
 
         // Turn Right
         if (Input.GetKey(KeyCode.D))
         {
             _turnRight = true;
             _animator.SetBool("TurnRight", true);
         }
         else
         {
             _turnRight = false;
             _animator.SetBool("TurnRight", false);
         }
 
         // Turn Left
         if (Input.GetKey(KeyCode.A))
         {
             _turnLeft = true;
             _animator.SetBool("TurnLeft", true);
         }
         else
         {
             _turnLeft = false;
             _animator.SetBool("TurnLeft", false);
         }
 
         // Starfe Right
         if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.D))
         {
             _strafeRight = true;
             _animator.SetBool("StrafeRight", true);
         }
         else
         {
             _strafeRight = false;
             _animator.SetBool("StrafeRight", false);
         }
 
         // Starfe Left
         if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.A))
         {
             _strafeLeft = true;
             _animator.SetBool("StrafeLeft", true);
         }
         else
         {
             _strafeLeft = false;
             _animator.SetBool("StrafeLeft", false);
         }
 
         // Jump
         if (Input.GetKey(KeyCode.Space))
         {
             _jump = true;
             _animator.SetBool("Jump", true);
         }
         else
         {
             _jump = false;
             _animator.SetBool("Jump", false);
         }
     }
 }
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 gjf · Jul 23, 2019 at 05:39 PM

You should be more concerned with the fact that your code doesn't compile. You cannot have two Update() functions!

That said, you could change each of your key checks and subsequent animator triggers to something like:

 _runForward = Input.GetKey(KeyCode.W);
 _animator.SetBool("RunForward", _runForward);

...but that makes it harder to change functionality. You reduce the code but it may be a waste of time until you have everything working correctly. It's known as "premature optimization". As you gain experience, you'll hopefully resist the temptation...

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

177 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

Related Questions

If else block acts extremely weird 0 Answers

Need a object to react different when clicked mutliple times 1 Answer

HELP ME PLEASE TO FIX MY PROBLEM.. 1 Answer

How to use IF in Unity for selection MyScript 1 Answer

Why is my if-statement still true when i set it to false, and wont move to the next else if-statement? 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