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 alb917 · Dec 29, 2012 at 12:23 AM · animationiossoundlooping

How can I stop animation and sound clip looping on tapCount

Hi there,

I have created a basic animation of my player (capsule) spinning when a button is pressed. I have also told a sound clip to play when the same button is pressed. I am developing my game for the iOS platform, so have used the Camera Relative Control setups, and given this animation + sound button the joystick script. Both the animation and sound clip play, but they are repeated until the button is released. Does anyone know how to make these clips play only once per tapCount?

Code is below:

 var rightJoystick : Joystick; 
 var sound : AudioClip;
 
 function Update()
 {
 
 if (rightJoystick.tapCount == 1 ) 
         {
          //Play Kill animation
         AudioSource.PlayClipAtPoint(sound, transform.position);
         animation["kill"].wrapMode = WrapMode.Once;
           animation.Play("kill");
         }
     
     }


This script is attached to the player, which the animation is also attached to. The script has the same effect with or without: animation["kill"].wrapMode = WrapMode.Once;

If anyone could help out it would be much appreciated!

Thanks

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 Codetastic · Dec 29, 2012 at 05:42 AM

Looking at the script 2 problems should be visible:

1) AudioSource.PlayClipAtPoint(sound, transform.position); Would be played over and over every frame thus creating a large number of audio sources.

2) animation.Play("kill"); Is also played over and over each frame. However it looks like it is looping because animation.Play() does not restart the current animation if the animation is already playing, and so when the animation does end it plays it yet again. See Animation.Play() Description 4th sentence: "If the animation is already playing, other animations will be stopped but the animation will not rewind to the beginning"

So all this is happening because you are telling it to do so in Update() (Update happens every frame). Thankfully all you need to fix this is to check if the joystick was just pressed or has bin down already like so:

 #pragma strict
 var rightJoystick : Joystick; 
 var sound : AudioClip;
 private var isJoystickDownAlready : boolean = false;
 
 function Update(){
     if(rightJoystick.tapCount > 0 && isJoystickDownAlready == false){ //if more then 0 we are being pushed
         //Set isJoystickDownAlready to true as rightJoystick was just being pushed down.  This will only happen once the button is pushed because in the above line we check if isJoystickDownAlready is set to false
         isJoystickDownAlready = true;
         //Play Kill animation
         AudioSource.PlayClipAtPoint(sound, transform.position);
         animation["kill"].wrapMode = WrapMode.Once;
         animation.Play("kill");
     }else if(rightJoystick.tapCount == 0){
         //Once rightJoystick is released, tapCount should become 0 so now now if tapCount is 0 set isJoystickDownAlready to false because the rightJoystick is no longer down
         isJoystickDownAlready = false;
     }
 }

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 alb917 · Dec 29, 2012 at 01:46 PM 0
Share

That worked a treat! Thanks so much :)

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I get a split-up sound to properly loop with an animation? 1 Answer

2017.1 and iOS music playback 0 Answers

loop animation while audio is playing 0 Answers

How to know if my speakers are mute? c# android/ios 0 Answers

Sound issue when app is launched by Callkit 0 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