• 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
Question by Dreave · Nov 09, 2011 at 05:44 PM · animationbuttonhold

Loop animation on button hold

how would I change this script so that if the LMB is held down it loops the animation until it has stopped being held but if pressed once it plays the animation once?

function Update () {

if(Input.GetButtonDown("Fire1")){

animation.Play("shoot");

}

}

Comment
The-Arc-Games

People who like this

1 Show 0
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
Best Answer

Answer by The-Arc-Games · Nov 10, 2011 at 06:26 AM

Alternatively, there's a simpler approach.

 function Update () {

 if(Input.GetButtonDown("Fire1")){
 
 myanimationisplaying = true;  //boolean value. declare it to false
 
 }
 
 if(Input.GetButtonUp("Fire1")){
 
 myanimationisplaying = false;
 
 }
 
 if (myanimationsiplaying == true)
   animation.Play("shoot");
 }
 //the rest of the code

Which is how you usually logically implement this kind of functionality.

Comment

People who like this

0 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 Dreave · Nov 10, 2011 at 04:12 PM 0
Share

im having problems getting it to work, I have no errors in the console
I have my script attached to my gun but when I press the LMB it only plays the animation once like before. Could you please help?

avatar image The-Arc-Games · Nov 13, 2011 at 09:32 AM 1
Share

this comment '//boolean value. declare it to false' actually meant that you need to declare the variable to be able to use it! 'var myanimationisplaying : bool;' and then set it to 'false' on start.

avatar image PhoneguyHELLOHELLO · Mar 12, 2016 at 10:53 AM 0
Share

Thanks i used this but for walking and used "vertical"

avatar image

Answer by Jehos · Nov 09, 2011 at 05:56 PM

You could change the WrapMode depending on the LBM being pressed.

Something in the vicinity of:

function Update()
{
  if (Input.GetButtonDown("Fire1"))
  {
    if (!animation.isPlaying)
    {
      animation.Play("shoot");
    }
  }
  animation["shoot"].wrapMode = Input.GetButton("Fire1") ? WrapMode.Loop : WrapMode.Once;
}

This will cause the animation to loop if the button is being held, and to stop looping when the button is not being held. Keep in mind when you release the mouse button, animation will not immediately stop, it will rather reach to the end and then stop.

Comment
The-Arc-Games

People who like this

1 Show 2 · 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 Dreave · Nov 09, 2011 at 06:12 PM 0
Share

This script dosent seem to do anything, I have checked the console for errors but theres nothing to blame in there, any ideas?

avatar image Jehos · Nov 09, 2011 at 07:52 PM 0
Share

It was meant as a guide rather than as a code.

The idea is that you change the WrapMode of your AnimationState depending on the user pressing or releasing the LMB:

If the user is pressing the LMB, animation should be set to Loop. If the user is not pressing the LMB, animation should be set to Once.

Animation start doesn't require anything special in coding, except to make sure you don't re-execute the Play() command while the animation is already playing.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

how to run Sprint 1 Answer

Stop animation when no more bullets 1 Answer

Loop Animation Help 1 Answer

Reloading Help 2 Answers

Animation Scripting Help 2 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