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 Leon123442 · Feb 20, 2018 at 07:06 PM · animationscripting problemscript.beginner

How can I improve this code?

I just wrote a simple direction based combat script. However, it feels unresponsive and I have the feeling that I used the code inefficiently.

How can I improve it?


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class attack : MonoBehaviour {
     static Animator anim;
     public bool attack1;
     public bool attack2;
     public bool attack3;
     public bool position1;
     public bool position2;
     public bool position3;
 
     IEnumerator attackone()
     {
         yield return new WaitForSeconds(1);
         attack1 = false;
     }
     IEnumerator attacktwo()
     {
         yield return new WaitForSeconds(1);
         attack2 = false;
     }
     IEnumerator attackthree()
     {
         yield return new WaitForSeconds(1);
         attack3 = false;
     }
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetAxis("Mouse X") > 0)
         {
             position1 = true;
             position2 = false;
             position3 = false;
 
         }
         if (Input.GetAxis("Mouse X") < 0)
         {
             position2 = true;
             position1 = false;
             position3 = false;
         }
         if (Input.GetAxis("Mouse Y") < 0)
         {
             position3 = true;
             position2 = false;
             position1 = false;
         }
         if (position1 == true)
         {
             if (Input.GetMouseButtonDown(0))
             {
                 attack1 = true;
             }
         }
         if (position2 == true)
         {
             if (Input.GetMouseButtonDown(0))
                 attack2 = true;
         }
         if (position3 == true)
         {
             if (Input.GetMouseButtonDown(0))
             {
                 attack3 = true;
             }
         }
         if (attack1 == true)
         {
             anim.SetBool("attackleft", true);
             anim.SetBool("idle", false);
             StartCoroutine("attackone");
         }
         if (attack2 == true)
         {
             anim.SetBool("attackright", true);
             anim.SetBool("idle", false);
             StartCoroutine("attacktwo");
         }
         if (attack3 == true)
         {
             anim.SetBool("attackup", true);
             anim.SetBool("idle", false);
             StartCoroutine("attackthree");
         }
         if (attack1 == false && attack2 == false && attack3 == false)
         {
             anim.SetBool("idle", true);
         }
         if (attack1 == false)
         {
             anim.SetBool("attackleft", false);
         }
         if (attack2 == false)
         {
             anim.SetBool("attackright", false);
         }
         if (attack3 == false)
         {
             anim.SetBool("attackup", 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
1
Best Answer

Answer by Alanisaac · Feb 21, 2018 at 01:39 AM

CharacterDirection enum

From reading your script, it looks like you're representing the different directions your character is facing with the variables position1, position2, etc. The way I would write this instead would be to use an enum. An enum gives you the advantage of being more concise (one variable instead of three) and more understandable (I can give the enum value meaning, like "Left", "Up", and "Right"). I called this enum CharacterDirection because that seemed like the concept it represented.

isAttacking

It also looks like your attacks are tied to position where attack1 represents an attack at position1 and so on. Since the attack is tied with the position, once you're using an enum you can condense the multiple attack variables into a simpler "isAttacking" boolean. I like using true/false words at the start of my boolean variables like "is", "can", or "should" because I find it helps make the variable name clearer.

Coroutine

With those changes -- using an enum and an "isAttacking" variable -- you also only need a single coroutine to handle the duration of the attack, so that can be condensed as well.

Start/Stop Animation Functions

Lastly, I notice you're setting a lot of different booleans on your animation in several different places. I think it might help to give those their own meaningful functions like StartAttackAnimation() and StopAttackAnimation(). This would let you better control exactly when the animation begins and ends, and might help you to figure out that unresponsiveness and get the exact behavior you want.

Script

The code I came up with looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public enum CharacterDirection
 {
     Left,
     Right,
     Up
 }
 
 public class Attack : MonoBehaviour 
 {
     static Animator anim;
     public bool isAttacking;
     public CharacterDirection characterDirection;
 
     IEnumerator attackWait()
     {
         yield return new WaitForSeconds(1);
         if(!isAttacking)
         {
             StopAttackAnimation();
         }
     }
 
     // Use this for initialization
     void Start() 
     {
         anim = GetComponent<Animator>();
     }
      
     // Update is called once per frame
     void Update() 
     {
         if (Input.GetAxis("Mouse X") > 0)
         {
             characterDirection = CharacterDirection.Left;
         }
         if (Input.GetAxis("Mouse X") < 0)
         {
             characterDirection = CharacterDirection.Right;
         }
         if (Input.GetAxis("Mouse Y") < 0)
         {
             characterDirection = CharacterDirection.Up;
         }
         if (Input.GetMouseButtonDown(0))
         {
             isAttacking = true;
             StartAttackAnimation();
             StartCoroutine("attackWait");
         }
         else 
         {
             isAttacking = false;
         }
     }
     
     private void StartAttackAnimation()
     {
         anim.SetBool("idle", false);
         anim.SetBool("attackleft", characterDirection == CharacterDirection.Left);
         anim.SetBool("attackright", characterDirection == CharacterDirection.Right);
         anim.SetBool("attackup", characterDirection == CharacterDirection.Up);
     }
 
     private void StopAttackAnimation()
     {
         anim.SetBool("idle", true);
         anim.SetBool("attackleft", false);
         anim.SetBool("attackright", false);
         anim.SetBool("attackup", false);
     }
 }

Note that the intent of this code is so that your player should be able to hold down the mouse button and continuously attack. If that's not what you want, you can play around with conditions for when to set isAttacking to false and when to call StopAttackAnimation();

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 Leon123442 · Feb 21, 2018 at 05:30 AM 1
Share

Thank you. I have one last question, Is there a way to replace the WaitForSeconds command with a command wich waits for the animation to finish?

avatar image Alanisaac Leon123442 · Feb 21, 2018 at 02:25 PM 0
Share

Check out this UA post. There are a number of answers there about how to wait for an animation to finish.

avatar image Leon123442 Alanisaac · Feb 21, 2018 at 04:00 PM 0
Share

I already tried it, but for some reason I cant get them to work.

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

254 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

Related Questions

Jump animation not returning to normal? 0 Answers

Scripts Interfering With Each Other 1 Answer

Move to next animation with clicking the game object 0 Answers

Opening Door pressing a key (keyboard or pad) 1 Answer

animation scirpting 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