Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 TusharP914 · Jul 12, 2015 at 12:07 PM · c#animationwrapmode

How to avoid animation from resetting after its completion?

I have a basic scene where I have a barrier like the ones at the tollbooth stations. Barrier has an animation where it goes from 0 degrees to 90 degrees in order to open and close the barrier. Now, I control the barrier such that pressing s plays the animation and w reverses the animation. Here is the Script:-

     public Animation cylinderAnim;
  
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.S))
         {
             cylinderAnim.GetComponent<Animation>()["CylinderMove"].speed=2;
             cylinderAnim.GetComponent<Animation>().Play("CylinderMove");
         }
         else if(Input.GetKeyDown(KeyCode.W))
         {
             cylinderAnim.GetComponent<Animation>()["CylinderMove"].speed=-1;
             cylinderAnim.GetComponent<Animation>().Play("CylinderMove");
         }
         else if((Input.GetKeyUp(KeyCode.S)) || (Input.GetKeyUp(KeyCode.W)))
         {
             cylinderAnim.GetComponent<Animation>()["CylinderMove"].speed=0;
             cylinderAnim.GetComponent<Animation>().Play("CylinderMove");
         }

But the problem is that it resets the animation after completion i.e. it jumps straight to 0 degree angle from 90 degree angle. I just want it to pause right there and not move anymore than 90 degrees. Now I know the culprit here is 'wrapmode.once' as it resets the animation. Loop and pingpong will also obviously not work. ClampForever comes very close to what I want to do but it does what its name suggests, it literally clamps forever i.e. if i keep the button s pressed, the animation will pause, but the clamp value just keeps on increasing, and thus if i press w to play animation in reverse, it will first decrease the clamp value and then starts playing the animation. I tried setting up the ontriggerstay/ontriggerenter at the other end so that if the barrier's end enters this trigger, it should stop like in this script:-

 void OnTriggerStay(Collider other) //or ontriggerenter
     {
         other.GetComponent<Animation>()["CylinderMove"].speed=0;
         other.GetComponent<Animation>().Play("CylinderMove");
     }

This works, but if i keep on tapping the 's' it will finally go through my trigger and then will either reset or clampforever depending on the wrapmode. Also, I cannot just place a collider to stop the barrier, because when the barrier hits the collider, it just goes off the rails and messes everything up. So, What do have to do to just pause the animation or restricting it between o and 90 degrees only. Also, can i set the min and max values for wrapmode.clampforever because that will also solve my problem. 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

Answer by Mehrdad995 · Jul 12, 2015 at 01:38 PM

I almost don't have any experience on animations so it might be much easier than my way.
if i were you i would try to use coroutine and yield just like when we want to play an audioclip and stop it after it played.

this script should work. let me know if anything got wrong.

 using UnityEngine;
 using System.Collections;
 
 public class AnimationPlayer : MonoBehaviour 
 {
     public Animation cylinderAnim;
     public AnimationState ast;
     bool animPlaying;
 
     void Start()
     {
         ast = cylinderAnim.GetComponent<Animation>()["CylinderMove"];
         StartCoroutine("PlayAnimOnButton");
     }
 
     IEnumerator PlayAnimOnButton()
     {
         while(true)
         {
             if(Input.GetKeyDown(KeyCode.S) && !animPlaying)
             {
                 yield return StartCoroutine(AnimPlay("CylinderMove",2.0f));
             }
             else if(Input.GetKeyDown(KeyCode.W) && !animPlaying)
             {
                 yield return StartCoroutine(AnimPlay("CylinderMove",-1.0f));
             }
             if((Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.W)) && animPlaying)
             {
                 ast.speed = 1.0f;
                 cylinderAnim.Stop(); // if you want to stop only a special anim, write its name between parentheses;
                 animPlaying = false;
             }
         }
     }
     IEnumerator AnimPlay(string animClipName, float speed)
     {
         animPlaying = true;
         cylinderAnim.Play(animClipName);
         ast.speed = speed;
         yield return new WaitForSeconds(ast.length);
         cylinderAnim.Stop();
         animPlaying = false;
         yield return null; // optional as long as we have the yield above
     }
 }
 
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 TusharP914 · Jul 15, 2015 at 08:33 AM 0
Share

Your solution didn't work, thanks for the help anyways. I solved it myself using vector3.angle and checked whether the angle was less than 1 degrees.:-)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Animation not playing completely 1 Answer

Multiple Cars not working 1 Answer

Programmatically created AnimationClip will not loop 2 Answers

Clamping an animation does not work. Help! 0 Answers

Distribute terrain in zones 3 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