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
0
Question by Blink · Mar 17, 2013 at 07:39 PM · animationloopgun

Stop looping animation?

My script tells my animated gun to disable the shooting animation after it has been played 30 times, this works fine except my gun is fully automatic. This means I want my script to know when my animation has looped 30 times instead of being played only once. This is my script, I hope I explained okay...

 function Start () {
 animation["reload"].speed = 2;
 animation["shoot"].speed = 10;
 animation["shoot"].wrapMode = WrapMode.Loop;
 animation["reload"].wrapMode = WrapMode.Once;
 animation["shoot"].layer = 1;
 animation["reload"].layer = 2;
 animation.Stop();
 
 StartCoroutine(ShotPoller());
 
 }
 
 function ShotPoller () {
 
 while(true){
 
     if(Input.GetButton("Fire1")){
         Shoot();
         
         yield WaitForSeconds(refireRate);
         
         }else{
    animation.Stop("shoot");
     }
     
     
     if(Input.GetKeyDown("r")){
         Reload();
     }
 
     yield;
 }
 }
 
 function Shoot (){
 
 if(Input.GetButton("Fire1")){
   if(bulletsLeft > 0 ){
      bulletsLeft -- ;
      animation.Stop();
      animation.Play("shoot");
    }else{
    animation.Stop();
    }
 }
 
 }
 
 var fullClip : int = 30;
 var bulletsLeft : int = 30;
 var waitTime = 2.5;
 var refireRate : float = 0.1;
 
 function Reload () {
 
 if(Input.GetButtonDown("r")){
    animation.Play("reload");
    yield WaitForSeconds(waitTime);
    bulletsLeft = fullClip ;
 }
 }
Comment
Add comment · Show 4
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 dorpeleg · Mar 17, 2013 at 09:44 PM 0
Share

I wander why you want to know the number of animations loop.

I'm guessing you want to stop it when you run out of ammo.

You should just check if you have more then zero ammo and only then play the animation.

avatar image Blink · Mar 17, 2013 at 09:47 PM 0
Share

How would I do this?

avatar image dorpeleg · Mar 17, 2013 at 09:56 PM 0
Share

hmmm... now that I look at your code again, it seems you are doing it allready.

 if(Input.GetButtonDown("Fire1")){
   if(bulletsLeft > 0 ){
      bulletsLeft -- ;
      animation.Stop();
   animation.Play("shoot");
    }
 }

Are you saying that the animation is still playing after your bulletsLeft is 0?

avatar image Blink · Mar 17, 2013 at 09:58 PM 0
Share

No the animation stops if it is at 0 but the number only goes down by one every time you click the left mouse button, I want it to continuesly go down until you stop holding the mouse button.

1 Reply

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

Answer by dorpeleg · Mar 17, 2013 at 10:03 PM

Use GetKey insted of using GetButtonDown.

GetKey

Description

Returns true while the user holds down the key identified by name. Think auto fire.

Comment
Add comment · Show 6 · 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 Blink · Mar 18, 2013 at 08:09 AM 0
Share

Now I have my number going down continuesly but my guns animation doesn't stop once it gets to 0 any ideas? I also updated my script since I have changed it a bit.

avatar image dorpeleg · Mar 18, 2013 at 03:35 PM 0
Share

Try this:

 if(Input.GetButton("Fire1")){
   if(bulletsLeft > 0 ){
      bulletsLeft -- ;
      animation.Stop();
      animation.Play("shoot");
    }else{
    animation.Stop();
    }
 }
avatar image Blink · Mar 18, 2013 at 04:09 PM 0
Share

Once the number gets to zero the animation stops but If I stop holding the mouse button when the ammo is above 0 it keeps playing the animation.

avatar image dorpeleg · Mar 18, 2013 at 05:04 PM 0
Share

Add this:

   if (Input.GetButtonUp ("Fire1")) {
         animation.Stop();
         }
avatar image Blink · Mar 18, 2013 at 05:22 PM 0
Share

I figured it out and updated my code, thanks for your help!

Show more comments

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

11 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

Related Questions

3rd person gta like weapon problem?? 0 Answers

Animation loop stop? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Animation On Mouse Click 1 Answer

Loop Animation In Script? 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