Search Unity

Animation is delayed after few play attemps even though all transition times' are zero

Discussion in 'Animation' started by rebel, May 16, 2020.

  1. rebel

    rebel

    Joined:
    Dec 22, 2013
    Posts:
    1
    Hi, 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.



    Code (CSharp):
    1.     private Animator eightAnimator;
    2.     private bool isLine;
    3.     private bool isMoving;
    4.     private bool becomingEight;
    5.     private bool becomingLine;
    6.  
    7.     private Vector3 nextPosition;
    8.     private Vector3 startPosition;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         isMoving = false;
    14.         isLine = false;
    15.         becomingEight = false;
    16.         becomingLine = false;
    17.         eightAnimator = GetComponent<Animator>();
    18.  
    19.         startPosition = this.transform.position;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if(Input.GetKeyDown(KeyCode.M))
    26.         {
    27.             eightAnimator.SetBool("isMoving", true);
    28.  
    29.             if (isLine)
    30.             {
    31.                 isMoving = true;
    32.                 isLine = false;
    33.                 becomingEight = true;
    34.                 becomingLine = false;
    35.                 nextPosition = new Vector3(this.transform.position.x + 6, this.transform.position.y, this.transform.position.z);
    36.                 eightAnimator.SetFloat("speedMultiplier", 1f);
    37.                
    38.             } else
    39.             {
    40.                 isMoving = true;
    41.                 isLine = true;
    42.                 becomingLine = true;
    43.                 becomingEight = false;
    44.                 eightAnimator.SetFloat("speedMultiplier", -1f);
    45.             }
    46.  
    47.         }
    48.  
    49.         if(becomingEight)
    50.         {
    51.             this.transform.position = Vector3.MoveTowards(this.transform.position, nextPosition, Time.deltaTime * 10);
    52.  
    53.             if (Vector3.Distance(nextPosition, this.transform.position) < 0.01f)
    54.             {
    55.                 isMoving = false;
    56.                 becomingEight = false;
    57.                 eightAnimator.SetBool("isMoving", false);
    58.             }
    59.         }
    60.  
    61.         if(becomingLine)
    62.         {
    63.             this.transform.position = Vector3.MoveTowards(this.transform.position, startPosition, Time.deltaTime * 10);
    64.  
    65.             if (Vector3.Distance(this.transform.position, startPosition) < 0.01f)
    66.             {
    67.                 isMoving = false;
    68.                 isLine = true;
    69.                 becomingLine = false;
    70.                 eightAnimator.SetBool("isMoving", false);
    71.             }
    72.         }
    73.  
    74.     }


    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?
     

    Attached Files: