Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 budin · Jun 12, 2011 at 11:20 AM · animationinputtriggerdisableend

Animation End as a trigger and how to disable input

So, I am fairly new to scripting, and I am having troubles with a sequence that I am trying to set up... ideally, it goes like this:

  1. when you press "f", the sequence starts, with an animation (looped) and a sound playing

  2. While these are playing, if you press "f" again, this does not affect the sequence

  3. when the sound finishes, the animation stops with it, and another, shorter sound plays

  4. after all this, if you press "f" again, it can start all over again

So, I've tried various approaches to these stages, and I'm encountering various problems...

  1. the end of the sound does not trigger what I want, or anything at all really

  2. when i've tried to stop the animation using a WaitForSeconds, it doesnt listen to it but ends the animation immidiately, doesnt wait at all

  3. whenever "f" is pressed again during the sequence, you can hear another instance of it start up in the background... its really annoying, if you keep pressing the key, it generates this huge bluster of noise from all the sequences happening at once

The code generating all this hullabaloo is below:

 var During : AudioClip;
 var After : AudioClip;
 
 function Update (){
      if (Input.GetKeyDown("f")){
           animation.Play("Flight");
           audio.PlayOneShot(During);
 
   
      if (!audio.isPlaying == false){
          Debug.Log("Are you done yet?");
          audio.PlayOneShot(After);
 }
     //WaitForSeconds(22);
         //animation.Stop("Flight");
         //audio.PlayOneShot(After);
     
     
 }

}

And there you have it. As you can see, the "WaitForSeconds" thing is a path I chose not to pursue but left there just in case it can be used...

If anyone could help my rather frustrating problems, I would be very grateful... Some of it is probably silly mistakes, but I will learn and know for next time :)

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
1

Answer by Kith · Jun 13, 2011 at 02:03 AM

Alright, let's see if we can tackle these problems one by one.

1) The end of the sound does not trigger what I want, or anything at all really

This is to be expected. Right now, your if statement for the audio (if (!audio.isPlaying == false)) is nested within the if statement above it (if (Input.GetKeyDown("f"))). The only way that the end of the sound will trigger anything is if you press the f key at the EXACT frame that the audio stops...which is darn near impossible. You're going to want to unnest those if statements for that to work properly. I think I see what you were trying to do though. Try this out.

 var began : boolean = false
 
     function Update (){
          if (Input.GetKeyDown("f")){
               animation.Play("Flight");
               audio.PlayOneShot(During);
               began = true;
          }
     
          if (!audio.isPlaying && began){
              Debug.Log("Are you done yet?");
              audio.PlayOneShot(After);
              began = false;
           }
     }



3) Whenever "f" is pressed again during the sequence, you can hear another instance of it start up in the background... its really annoying, if you keep pressing the key, it generates this huge bluster of noise from all the sequences happening at once

With the code we added in our previous step, this could be an easy fix. Replace

 if (Input.GetKeyDown("f")){

with...

 if (Input.GetKeyDown("f") && !began){

This will only fire when both conditions are met (The button is pressed AND the animation hasn't already begun).

2) When i've tried to stop the animation using a WaitForSeconds, it doesnt listen to it but ends the animation immidiately, doesnt wait at all

This one is going to be a little tricky to answer, but I'll give it a shot. Whenever you want to "WaitForSeconds(x)", you want to add a yield statement before it. Otherwise, all you're doing is telling the CPU to count to x, but it's going to keep doing everything else that it's doing while counting. Yield actually makes it stop what it's doing while it counts to x. Now, having said that, you can't have any yield statements in the Update() function. Update never yields; it's called every frame. What you can do is, instead of having everything written directly into the Update function, do something like this.

 function Update () {
      CheckInput();
 }

 function CheckInput() {
      //Your code with the yield WaitForSeconds(22); in there somewhere.
 }

It was a pretty long answer, and I hope I haven't made any mistakes in it, but it should definitely get you going in the right direction. Good luck!

-Kith

Comment
Add comment · Show 4 · 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 budin · Jun 13, 2011 at 02:47 AM 0
Share

Thankyou so much for helping! :) One problem with the code was that whenever I pressed "f", both "if"'s started at once, if you get me... the During and After clips both played when the key was pressed... I experimented around with the " if (!audio.isPlaying && began){ " line and I'm pretty sure there's a problem in there somewhere... also when I added your line of "&& began" into the input.getkeydown line, it was as if it had no effect :S

$$anonymous$$aybe I'll try the WaitForSeconds line, but I still need the waiting to start when the audio clip starts, and not when the whole scene itself starts, you know?

avatar image Kith · Jun 13, 2011 at 02:52 AM 0
Share

Glad to...try to help lol. Well, remember, the input.getkeydown line needs an && !began, not && began. And if you do decide to use the WaitForSeconds, you can easily make the waiting start when the audio clip starts by putting the yield statement in the If (input.getkeydown) statement.

avatar image budin · Jun 13, 2011 at 02:52 AM 0
Share

I FOUND IT!!! :D

I used this code and it worked PERFECTLY!!!!

var During : AudioClip; var After : AudioClip;

var begin : boolean = false;

function Update (){ if (Input.Get$$anonymous$$eyDown("f") && begin == false){ animation.Play("Flight"); audio.PlayOneShot(During); begin = true; CheckInput (); }

}

function CheckInput () { if (Input.Get$$anonymous$$eyDown("f")){ yield WaitForSeconds(22); animation.Stop("Flight"); audio.PlayOneShot(After); begin = false; } }

Thankyou so much for your help, I'm pretty much dancing on the table right now!!!! :D:D

avatar image Kith · Jun 13, 2011 at 02:56 AM 0
Share

Lmao no problem! Congratulations :-)

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

2 People are following this question.

avatar image avatar image

Related Questions

Animation Keypress Trigger Problem 0 Answers

disable all inputs 1 Answer

Clear mecanim trigger input? 1 Answer

How to trigger animaton on two different Game Object's at the same time? 1 Answer

how to make my component disabled after an animation 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