Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question jump to previous sprite in animation

Discussion in 'Animation' started by lz7cjc, Feb 24, 2024.

  1. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    546
    Hi
    i have created a very simple animation of photos. I am using .speed = 0 and 1 to start/stop the animation.
    Code (CSharp):
    1. anim.speed = 0;
    2. anim.speed = 1;
    When the animation is paused i want to offer the ability to go back to the previous photo in the series

    I have been searching for solutions but I don't think i am searching for the correct terminology!

    I basically want to do this
    sprite/frame = x
    moveto (sprite/frame - 1)

    how do i do this?

    thanks
     
  2. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    546
    to put it another way
    I have an animation with 30 sprites. I have paused the animation at sprite 18. How do go back to sprite 17?

    (also tried chat gpt but that was worse than useless!)

    thanks
     
  3. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    211
    You could try using the Animator.Play() function.
    https://docs.unity3d.com/ScriptReference/Animator.Play.html

    One of the parameters is normalizedTime which tells the animator where to start the animation. In your example, you would pass in 18/30 to start at frame 18. I don't know how this will behave if you have set the Animator speed to 0.
     
  4. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    546
    thank you - that has definitely set me on the right track
    Code (CSharp):
    1. anim.Play("Base Layer.editPhotos", 8/14, 0.25f);
    However if I am going to use this method then i need to be able to dynamically work out the current frame, ideally the total number of frames and then decrement

    so it would look something like
    Code (CSharp):
    1. anim.Play("Base Layer.editPhotos", (currentSprite-1)/totalSprites, 0.25f);
    do you know if i am able to access those properties and if so, how?

    thanks
     
  5. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    211
    You may be able to use GetCurrentAnimatorStateInfo to get some of the information you are looking for, but this isn't going to tell you how many keyframes are defined for the current animation.
    https://docs.unity3d.com/ScriptReference/Animator.GetCurrentAnimatorStateInfo.html

    Are you sure that using an animator is the best option for your use case? It sounds like you need a lot of control over how and when the current sprite changes.
     
  6. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    546
    I'm beginning to think the same thing but seemed the easiest way to implement a simple photo frame. Will look for other options
    Thanks