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 EtherealPineapple · May 17, 2016 at 04:20 AM · c#animationscripting problemboolean

C# | How to detect the end of an animation?

Right, so I have 2 animations for a gun aiming. When I right click, the sights of the gun line up, and in the script the bool "gunAiming" becomes true. When it becomes true, and I right click again, the animation of the gun returning to it's original spot must be played and the bool will become false again. But the problem is that only the return animation is playing, and not the first aiming animation. Now, I think the problem is that the bool "gunAiming" becomes true immediately after pressing right-mouse button. So I think that the solution would be for the bool to become true once it detects that the animation ended. If that is not the problem, please provide another solution. Also, an in-depth explanation of what is happening in the script would be helpful. Here is the script:

     public bool gunAiming = false;
 
 void Update () {
         if (Input.GetKey (KeyCode.Mouse1) && !gunAiming) {
             GetComponent<Animation>().Play ("aim");
             gunAiming = true;
 
 
         }
         if (Input.GetKey (KeyCode.Mouse1) && gunAiming) {
             GetComponent<Animation> ().Play ("returnaim");
             gunAiming = false;
 
         }
     
     }
Comment
Add comment · Show 2
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 Cherno · May 17, 2016 at 07:24 AM 0
Share

Add an AnimationEvent to the clip that calls a function which sets the gunAi$$anonymous$$g bool to true or false.

avatar image superbsumit · May 17, 2016 at 08:22 AM 0
Share

You could do it like:

 public bool gunAi$$anonymous$$g = false;
 public Animation anim;
 
 void Start() {
      anim = GetComponent<Animation>();
 } 
 
  void Update () {
          if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.$$anonymous$$ouse1)) {
             if (!anim.IsPlaying("aim")) {
                  anim.Play ("aim");
             }
              if (!gunAi$$anonymous$$g) {
                  gunAi$$anonymous$$g = true;
                  // Also can do some stuff
              }
          } else {
              if (!anim.IsPlaying("mouseOverEffect")) {
                   anim.Play ("returnaim");
              }
              if (gunAi$$anonymous$$g) {
                   gunAi$$anonymous$$g = false;
                   // Also can do some stuff
             }
        }     
 }

1 Reply

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

Answer by gaming_man_is_gaming · Apr 13, 2021 at 12:40 PM

not sure if this fixes it but where it says Input.GetKey you have to put at the end .down. so it would be

      if (Input.GetKeyDown(KeyCode.Mouse1) && !gunAiming) {
          GetComponent<Animation>().Play ("aim");
          gunAiming = true;

then Input.GetKeyUp() for second one. But i wouldnt use animations in general because it can be unreliable. instead i would put a game object in front of your camera then one at the unaimed state. then create a script to set the position of the gun to that the game object infront of your camera then when not aiming in go back to the unaimed state. if you need help with the script just reply to my answer and ill put it in

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 EtherealPineapple · Apr 13, 2021 at 07:47 PM 0
Share

Thank you for your response! Hahaha! After five years, you have answered my question. This project is unfortunately no more, though I will accept your answer.

avatar image gaming_man_is_gaming EtherealPineapple · Apr 13, 2021 at 07:57 PM 0
Share

well im just glad it helped

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

6 People are following this question.

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

Related Questions

how to keep track of Lean Tween function? 0 Answers

UNITY 2D: How to make a level selection menu (like the video in details)? 1 Answer

Animator not working properly 1 Answer

Can't get boolean of an animator from other class 0 Answers

Armature bone unable to rotate while animation playing 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