Search Unity

Resolved Animator state playing stop after lerping. How to continue the playing ?

Discussion in 'Scripting' started by JuanaAlbez, Jun 13, 2022.

  1. JuanaAlbez

    JuanaAlbez

    Joined:
    Jun 13, 2022
    Posts:
    11
    When the lerping is over and t value reached 1 the anim.Play stop playing the state animation.
    Instead it should keep playing it on maximum value.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class MoveTurn : MonoBehaviour
    7. {
    8.     public float moveSpeed;
    9.     public float rotationSpeed;
    10.  
    11.     private Animator anim;
    12.     public float minimum = 0.0F;
    13.     public float maximum = 1.0F;
    14.     public float duration;
    15.  
    16.     // starting value for the Lerp
    17.     private float t = 0.0f;
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         anim = GetComponent<Animator>();
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         transform.Rotate(Vector3.up * Time.deltaTime * rotationSpeed);
    29.  
    30.         t += Time.deltaTime / duration;
    31.         float value = Mathf.Lerp(minimum, maximum, t);
    32.         anim.Play("Walk",0,value);
    33.     }
    34. }
    35.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I've never seen animation attempted this way.

    Usually you let the animation itself loop but you are preventing that by supplying value equal to maximum every frame after the lerp finishes up.

    You may wish to review the basic MecAnim setup tutorials... They usually just change properties on the Animator, such as
    .SetFloat("Speed", speed);
    for example.

    And of course you make that State use a float parameter to control its speed.

    Here's an example where I use my "Speed" parameter... this is within the animator State for my walk.

    Screen Shot 2022-06-13 at 3.29.54 PM.png
     
    JuanaAlbez likes this.
  3. JuanaAlbez

    JuanaAlbez

    Joined:
    Jun 13, 2022
    Posts:
    11
    Working. thanks.
     
    Kurt-Dekker likes this.