Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Animation WrapMode Once - Issue HELP!

Discussion in 'Editor & General Support' started by Entity3Limited, Jan 10, 2012.

  1. Entity3Limited

    Entity3Limited

    Joined:
    Jan 10, 2012
    Posts:
    21
    Hi,

    I am having an issue with a button and a linked animation.

    What I want to achieve is:
    when the button is held down the animation goes forwards, when released it reverses from where it is.

    This works.. However..
    If you hold the button and the animation reached the last frame it goes back to frame 1.

    I do not want this. I need it to hold on the last frame and goes in reverse from there when the button is released.
    so if there are 180 frames, holds on frame 180 when it get there.

    ClampForever will not work because the longer you hold at the end the longer it takes to reverse.

    PLEASE HELP!!

    My current code is below

     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Code (csharp):
    1. if (Input.GetMouseButton(0))
    2.  {
    3.  theMainCharacter.animation["Anim1"].speed = 1;
    4.  theMainCharacter.animation.Play("Anim1");
    5.  }
    6.  else {
    7.  theMainCharacter.animation["Anim1"].normalizedTime = Mathf.Clamp01 (  theMainCharacter.animation["Anim1"].normalizedTime );
    8.  theMainCharacter.animation["Anim1"].speed = -1;
    9.  theMainCharacter.animation.Play("Anim1");
    10.  }
    Try that perhaps?
     
  3. Entity3Limited

    Entity3Limited

    Joined:
    Jan 10, 2012
    Posts:
    21
    Hi,
    Thank you for your input but this does not work. This just stops all animations.

    It appears you placed the code on the reverse animation, there no issue on the reverse animation.

    The forwards animation is the issue. When the forwards animation reaches the last frame it goes back to fram 1, how to stop this??
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,002
    Switch to clamp forever? Then though I don't think the animation ever stops playing.
     
  5. Entity3Limited

    Entity3Limited

    Joined:
    Jan 10, 2012
    Posts:
    21
    No, As I said I dont want that to happen.

    The trouble with ClampForever is that it repeats the last frame. So if the button is held till the end of the animation then the last frame loops until the button is lifted. So if it loops the last frame 30 times, when the button is let go it also reverse through 30 still frames until the animation actualy starts to reverse.

    Anyone got the genuis to solve this one?
     
  6. Entity3Limited

    Entity3Limited

    Joined:
    Jan 10, 2012
    Posts:
    21
    Ok, So I googled, I tested, I googled, I tested, I read forums, I read manuals, 24 hours later if found a fix of sorts.
    The below code is what I have used, just incase anyone else has this issue.
    You must set the animation to ClampForever, but with some addtional code added.
    I am pleased that is over!

     
    bakos likes this.
  7. bakos

    bakos

    Joined:
    Dec 18, 2013
    Posts:
    1
    Dude.. I googled and looked up fixes for my issue for AGES.. you are a genius.
    I have to say this. Sorry to revive this post but you are a GENIUS!!!

    I mean, I was having the same exact issue for 2 days!!! And you just helped me out.. i was about to give up.

    Thank you so much sir
    If i could give you reddit gold, i would.