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 /
  • Help Room /
avatar image
0
Question by blackshtormx · May 11, 2016 at 07:29 PM · animationloop

prevent animation loop.

So I have problem with script, I just need to play zoom in animation when right mouse button is pressed, but it starts looping, here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class testAnimScript : MonoBehaviour {
 
     //Holder
     public Transform WalkAnimationHolder;
 
     public bool running = false;
     public float interval = 4.5f;
     public bool zoomed = false;
     public WalkingState walkingState = WalkingState.Idle;
 
     public void FixedUpdate()
     {
         AnimationController();
         SpeedController();
     }
 
     public void SpeedController()
     {
         //When we press right mouse button....
         if (Input.GetMouseButtonDown(1) && !running && !zoomed)
         {
             zoomed = true;
             walkingState = WalkingState.Zoom;
         }
         if (Input.GetMouseButtonUp(1))
         {
             zoomed = false;
         }
 
         //other...
         if ((Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) && !running && !zoomed)
         {
             if (Input.GetButton("Run"))
             {
                 walkingState = WalkingState.Running;
             }
             else
             {
                 walkingState = WalkingState.Walking;
             }
         }
         else
         {
             if (!running && !zoomed)
             {
                 walkingState = WalkingState.Idle;
             }
         }
     }
 
     public void AnimationController()
     {
 
         if(walkingState == WalkingState.Zoom)
         {
             // As I think here is my problem, basically to prevent animation loop, i do something like that: if it's time is more than 0.15 secs then, make it 0.15 so it will play once.
             if (WalkAnimationHolder.GetComponent<Animation>()["ak47zoom"].time >= 0.15f)
             {
                 WalkAnimationHolder.GetComponent<Animation>()["ak47zoom"].time = 0.15f;
              
                 Debug.Log(WalkAnimationHolder.GetComponent<Animation>()["ak47zoom"].time);
             }
             WalkAnimationHolder.GetComponent<Animation>().CrossFade("ak47zoom", 0.2f);
 
         }
         if (!zoomed)
         {
             if (walkingState == WalkingState.Running)
             {
                 WalkAnimationHolder.GetComponent<Animation>()["ak47run"].speed = 0.5f;
                 WalkAnimationHolder.GetComponent<Animation>().CrossFade("ak47run", 0.2f);
             }
             else if (walkingState == WalkingState.Walking)
             {
                 WalkAnimationHolder.GetComponent<Animation>()["ak47walk"].speed = 0.5f;
                 WalkAnimationHolder.GetComponent<Animation>().CrossFade("ak47walk", 0.2f);
             }
             else
             {
                 WalkAnimationHolder.GetComponent<Animation>().CrossFade("ak47breath", 0.2f);
             }
             if (walkingState == WalkingState.Reload)
             {
                 WalkAnimationHolder.GetComponent<Animation>().CrossFade("ak47reload", 0.2f);
             }
         }
         
     }
     public enum WalkingState
     {
         Idle,
         Walking,
         Running,
         Zoom,
         Shoot,
         Reload
     }
     void reload()
     {
         if (!running)
         {
             StartCoroutine(reloadGun());
         }
     }
     private IEnumerator reloadGun()
     {
         running = true;
         walkingState = WalkingState.Reload;
         yield return new WaitForSeconds(interval);
      
         running = false;
     }
 }
 

here is link to my video btw: youtube video

any help, comment, etc will be helpful! if code isn't readable just comment, I will try to make it smaller. :)

thanks.

-Nick

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 incorrect · May 12, 2016 at 03:45 AM 0
Share

FixedUpdate() is used for physics, better not try to use it for getting input, you will miss some. You should use Update() insted.

You can use switch operator ins$$anonymous$$d of that amount of if-s.

avatar image incorrect · May 12, 2016 at 03:46 AM 0
Share

Also I'd suggest not to do GetComponent every frame cause this approach can drop your FPS significantly once there will be some number of scripts doing it.

avatar image incorrect · May 12, 2016 at 03:49 AM 0
Share

Looks like it loops because once you do walkingState = WalkingState.Zoom;, you never change it back to any other state.

avatar image blackshtormx · May 12, 2016 at 04:56 AM 0
Share

Hi! thanks for your comments, yes I moved getmousebutton into the update, and I actually change walkingstate, in this part: if ((Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) && !running && !zoomed) { if (Input.GetButton("Run")) %|-1184559434_3|% walkingState = WalkingState.Running; } else %|-1421267895_7|% %|363040906_8|% %|-1230046081_9|% } ``but I don't think I understand what you mean.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I delete objects in an animation in a certain order? 1 Answer

Can not select loop time or any other stuff 0 Answers

Animation Panel: how to turn an existing loop into just a standing sprite and then implement it 0 Answers

When walking animation loops in timeline, it resets the player´s position back to the start 0 Answers

How to get perfect position animation with root motion? 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