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 Dreave · Nov 12, 2011 at 01:53 PM · animationloop

Animation Scripting Help

I have wrote this script which is supposed to loop an animation when the LMB is held down and when let go stops the animation. But it only plays my animation once, im not sure what ive missed out can someone please help me?

 function Start () {



 
 animation["shoot"].speed = 2.5;
 
 
 
  animation.wrapMode = WrapMode.Loop;
 
  
 
 }
 
 
 
 function Update () {
 
 
 
 if ( Input.GetButtonDown("Fire1")) {
 
 
 
    transform.animation.Play("shoot");
 
    
 
 if ( Input.GetButtonUp("Fire1")) {
 
 
 
    transform.animation.Stop("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

1 Reply

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

Answer by SteveFSP · Nov 12, 2011 at 08:27 PM

It looks like the problem is that you are setting the wrap mode on the animation object rather than the clip's animation state.

The code where you you are setting the wrap mode should be as follows:

animation["shoot"].wrapMode = WrapMode.Loop;

Edit: There is a second, unrelated problem with the code. (It doesn't have an impact on your looping issue.) Here is the correct code to get the behavior you are looking for.

function Start () {

 animation["shoot"].speed = 2.5;
 animation["shoot"].wrapMode = WrapMode.Loop;

}

function Update () {

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

    transform.animation.Play("shoot");

 }

 // In the original code you have this inside the button down check,
 // so the button up is never detected.
 if (Input.GetButtonUp("Fire1")) {

    transform.animation.Stop("shoot");  

 }

}

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

I have done this but it still dosent loop, is there anything I need to do outside the script?

avatar image Owen-Reynolds · Nov 13, 2011 at 03:03 PM 0
Share

If you want, you can go to the Asset, in the Inspector, and select Loop in the dropDown for "shoot" (the checkbox next to it is for smoothing -- can leave it off.) That does that same thing as the code line above.

avatar image SteveFSP · Nov 13, 2011 at 05:32 PM 0
Share

I found a second issue with your code. (See edit.) I've also run the fixed code in my own project. The looping and start/stop works as expected. If you are still having problems, it doesn't look like it is the code.

avatar image SteveFSP · Nov 13, 2011 at 05:40 PM 0
Share

Oh, and to answer your comment: $$anonymous$$y tests used a generic non-looping animation. No need to do any special setup. Just added the clip to the game object's animation component and attached the above script. Everything worked fine. At the moment, I can't think of any reason it isn't working for you.

avatar image killfast27 · Nov 02, 2013 at 09:06 PM 0
Share

if i wanted to transfer this to a walk animation so that when i press "w" would i just change Fire1 to w? cause when i tried changing Fire1 to w it didnt work

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Loop Animation In Script? 2 Answers

Loop Animation Help 1 Answer

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

Animation On Mouse Click 1 Answer

Aim down sights animation help 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