0
\$\begingroup\$

I am trying to play animation forward and backward when player press the same button again and again. I have a straight line and I want to convert it to 8 while moving it. I want to play animation backward or forward from the exact frame where I pressed the button again.

When I pressed the button, animation starts playing and moving and If I pressed the button again in the middle of animation it is played backward from the exact frame. However, after a few try my animation starts playing with a delay or it stops playing completely.

I am sending speedMultiplier parameter to make the speed -1 to play backward my animation.

This is the video of the problem

my transitions settings

private Animator eightAnimator;
private bool isLine;
private bool isMoving;
private bool becomingEight;
private bool becomingLine;

private Vector3 nextPosition;
private Vector3 startPosition;

// Start is called before the first frame update
void Start()
{
    isMoving = false;
    isLine = false;
    becomingEight = false;
    becomingLine = false;
    eightAnimator = GetComponent<Animator>();

    startPosition = this.transform.position;
}

// Update is called once per frame
void Update()
{
    if(Input.GetKeyDown(KeyCode.M))
    {
        eightAnimator.SetBool("isMoving", true);

        if (isLine)
        {
            isMoving = true;
            isLine = false;
            becomingEight = true;
            becomingLine = false;
            nextPosition = new Vector3(this.transform.position.x + 6, this.transform.position.y, this.transform.position.z);
            eightAnimator.SetFloat("speedMultiplier", 1f);

        } else
        {
            isMoving = true;
            isLine = true;
            becomingLine = true;
            becomingEight = false;
            eightAnimator.SetFloat("speedMultiplier", -1f);
        }

    } 

    if(becomingEight)
    {
        this.transform.position = Vector3.MoveTowards(this.transform.position, nextPosition, Time.deltaTime * 10);

        if (Vector3.Distance(nextPosition, this.transform.position) < 0.01f)
        {
            isMoving = false;
            becomingEight = false;
            eightAnimator.SetBool("isMoving", false);
        }
    }

    if(becomingLine)
    {
        this.transform.position = Vector3.MoveTowards(this.transform.position, startPosition, Time.deltaTime * 10);

        if (Vector3.Distance(this.transform.position, startPosition) < 0.01f)
        {
            isMoving = false;
            isLine = true;
            becomingLine = false;
            eightAnimator.SetBool("isMoving", false);
        }
    }

}

I am adding here unity package, video, screen shot and my simple code.

So how can I play my animation when I pressed the button with the exact frame and exact time without delay

This is the project unitypackage file

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

In your move.cs file, add this at the very top of the Update() method:

if(!becomingEight && !becomingLine){
    eightAnimator.SetFloat("speedMultiplier", 0f);
}

Although I have to say, this is all quite a bit of a hack. A much better way would be to have 2 animation states (eight and line) and toggle between them in the animator using animation transitions. But that would be almost a complete rework of your project and far outside the scope of your question.

P.S. Not in your question, but I noticed the very first time pressing "M" after running the project nothing happens. To fix this, change "isLine = false" to "isLine = true" in your Start() method.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you it worked :) \$\endgroup\$
    – cgrgcn
    May 17, 2020 at 19:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .