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
1
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
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
0
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
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 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 L$$anonymous$$B 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
1

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
Add comment · 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 Wrap$$anonymous$$ode of your AnimationState depending on the user pressing or releasing the L$$anonymous$$B:

If the user is pressing the L$$anonymous$$B, animation should be set to Loop. If the user is not pressing the L$$anonymous$$B, 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.

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

When my button is pressed the action reapeats 1 Answer

Third Person Help 4 Answers

Animation Problems 1 Answer

Character controller problem? 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