Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Tageos · Jul 19, 2015 at 04:01 PM · c#animationsprite-animation

Swap sprite for a set amount of seconds

Hi!

I need some coding advice on a attack animation

What i want to do is when lmb is clicked (mouse0) the PitchforkIdle sprite changes to the PitchforkAttack sprite for 2s. (The PitchForkAttack sprite is suppose to be an animation but for now i use single sprites). When the 2s has passed, the PitchForkAttack sprite changes back to the PitchForkIdle sprite. After the "animation" for 2s is done you cant do the attack again for 1s (so you wont be able to spam the attack). You will also not be able to to the attack again during the "animation".

This is what i have so far:

 using UnityEngine;
 using System.Collections;
 
 public class SpriteHandler : MonoBehaviour {
 
     public Sprite PitchforkIdle;
     public Sprite PitchforkAttack;
     private SpriteRenderer spriteRenderer;
 
     void Start () {
 
         spriteRenderer = GetComponent<SpriteRenderer>();
 
     }
 
     void Update () {
 
         if (Input.GetKeyDown (KeyCode.Mouse0)) // If lmb is pushed down
         {
             spriteRenderer.sprite = PitchforkAttack;
         }
 
         else
 
         {
             spriteRenderer.sprite = PitchforkIdle; // otherwise change it back to idle
         }
     }
 }

When i click mouse0 using this script i only get the Attack sprite for a split of a second.

I guess what i need is couroutine and yield.waitforseconds? I have looked in to these but dont really now how to implement them on my problem.

Anyhow i hope someone can give me some advice on this.

Cheers!

/Taegos

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Alec-Slayden · Jul 19, 2015 at 04:47 PM

Your sprite is only appearing in one frame because you are using GetKeyDown(), which is only true for the first frame a key was depressed. In circumstances where you want to continuously check a state you should use GetKey() instead.

However, I think that you should keep using GetKeyDown, because it sounds like you want your attack sprite to always last two seconds, therefore it doesn't matter if they keep holding the key. Instead of changing that, lets change something else. For one thing, we're not going to want the else condition in there like that, because you're changing the sprite every frame unnecessarily.

A coroutine would work, but since you're already using update and it's a simple script, lets just add to that.

First, we want to know if we have started attacking, so lets add a bool field called isAttacking. You could alternatively just check to see which sprite is being used, but I find a value specifically designed for a state check to be more friendly to alterations to the other fields later.

Next we want to know how long we've been attacking, so lets add a float called attackTimer. We'll add Time.deltaTime into it so it will give us the seconds that have passed.

Now what we'll do is have a key down event set the attacking flag to true. Then we're going to check to see if we're attacking, and if so, the timer will count up until it reaches 2 seconds. Once it's two seconds we'll reset the sprite and stop attacking.

 using UnityEngine;
  using System.Collections;
  
  public class SpriteHandler : MonoBehaviour {
  
     public Sprite PitchforkIdle;
     public Sprite PitchforkAttack;
     private SpriteRenderer spriteRenderer;
 
     private bool isAttacking;
     private float attackTimer;            
 
     void Start () {
         spriteRenderer = GetComponent<SpriteRenderer>();
     }
  
     void Update () {
 
         // set attack sprite and flag the frame that a key is down.
         if (Input.GetKeyDown (KeyCode.Mouse0)) 
         {
             this.isAttacking = true;
             this.spriteRenderer.sprite = PitchforkAttack;
           //this.attackTimer = 0f;  //option to reset time
         
         }
         
         // while attacking, count up the timer.
         if (this.isAttacking)
         {
             this.attackTimer += Time.deltaTime;
 
             // once the timer is 2 seconds, stop attacking and reset the sprite.
             if (this.attackTimer >= 2f)
             {
                 this.isAttacking = false;
                 this.attackTimer = 0f;
                 spriteRenderer.sprite = PitchforkIdle; 
             }
         }
     }
  }

If you want to reset the timer so it is 2 seconds again even if the user clicks in the middle of an attack, just set the timer to zero in the first condition, where the key is down.

The code above is sparse for readability here, but I suggest formatting it tighter for maintainability when used (remove extra line breaks etc)

Comment
Add comment · Show 1 · 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 Tageos · Jul 19, 2015 at 06:03 PM 0
Share

Wow, thank you so much! This is one of the best answers i have gotten since i signed up here. Very informative and noob-friendly ;) Thanks!

avatar image
2

Answer by etopsirhc · Jul 19, 2015 at 04:35 PM

coroutines. spicificaly make a coroutine that has both the animation and the wait time in it, then use an getbutton check to see if it needs to start the animation.

 coroutine function
     while (item equipped) // or something to make sure it only can trigger when you want it to 
         if (button down) // trigger animation
             start animation
             yeild for 2 seconds
             stop animation
             yeild for 2 seconds



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

23 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

Related Questions

2D Character Animation Help 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to call an attack animation that will stop when you stop clicking attack button? 1 Answer

UNITY 2D: How to make a level Selection Menu Using DoTween? 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