Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity offset cycle always starts from beginning?

Alright, I am trying to start my animation clip from my animation controller in Unity in C# from a different position in the clip (an offset) every time it is triggered, but Im not understanding how cycle offset works (clearly) and my clip aways seems to start from the beginning regardless.

What I have is a walk cycle that is set off by a bool depending on if character is moving, but Im trying to have the character alternate (have it play the full walk cycle but through the increments of the animation that is played since it is played only when character is moving) their left and right steps. Otherwise it looks weird

How Ive tried to do this is have a float and increase that float ever time they move, and assign that float to the cycle offset of my animation. That way I figure the next time the animation is triggered it will start where it left off.

I am unsure if offset refers to time, frames, or percentage, so I've just done this so far:

if (animOffset >= 1f) { //was full clip
            animOffset = 0.0f;
        }

        //anim help
        currentPos = transform.position;

        if (currentPos != lastPos) {
            //print ("moving now");
            animator.SetBool ("isWalking", true);
            if (InputManager.stepCount % 2 != 0) {
                print ("moving right");
                animator.SetFloat ("walkOffset",animOffset);

            } else {
                print ("moving left");
                animator.SetFloat ("walkOffset",animOffset);

            }
            animOffset += 0.1f;
        } else {
            animator.SetBool ("isWalking", false);
        }
        lastPos = currentPos;

Problem is Im not getting the desired effect and looking at the animation controller it seems the clip ALWAYS goes from the beginning, see:

enter image description here

The little blue bar always starts from beginning. What am I doing wrong here? How can I make an accurate run cycle offset?

like image 872
blue Avatar asked Jun 08 '26 19:06

blue


1 Answers

You can link the "Cycle Offset" on the animation to a parameter on the Animator (in this case I've called Offset). You can then set this parameter in code:

Here's the animator window (ignore Speed, it's not needed here). I have one state OnState.

The animator window showing the onstate

Here's the inspector window for the OnState state. I've ticked Parameter next to Cycle Offset and then it let me choose my parameter Offset.

The inspector window for the OnState state

In this code I get the animator and set the property Offset, which in turn can be set either programmaticly or through the inspector.

public class MyObject : MonoBehaviour
{
  public float Offset= 0f;
  Animator animator;

  void Awake()
  {
    animator = GetComponent<Animator>();
    animator.SetFloat("Offset", Offset);
  }

Offset is a value between 0 and 1, so you would find where in your animation where your feed move and set that as a proportion of 0 and 1.

like image 192
Dr Rob Lang Avatar answered Jun 11 '26 20:06

Dr Rob Lang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!