0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class FireEffect : MonoBehaviour
{
    public Transform player;
    public Transform target;

    private float timeElapsed = 0;
    private float lerpDuration = 3;
    private float startValue = 1;
    private float endValue = 0;
    private float valueToLerp = 0;

    private Animator playerAnimator;
    private bool prevFacing = false;
    private bool stopped = false;
    private bool move = true;
    private bool rot = false;
    private Vector3 currPos;

    private void Start()
    {
        playerAnimator = player.GetComponent<Animator>();
    }

    private void Update()
    {
        var currFacing = IsFacing(target);
        if (currFacing != prevFacing)
        {
            // here you switched from facing to not facing or vise verca.
            timeElapsed = 0;
        }
        prevFacing = currFacing;

        var distance = Vector3.Distance(player.position, target.position);

        if (IsFacing(target))
        {
            if (distance < 10 && move)
            {
                if (timeElapsed < lerpDuration)
                {
                    valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
                    playerAnimator.SetFloat("Forward", valueToLerp);
                    timeElapsed += Time.deltaTime;
                }
                playerAnimator.SetFloat("Forward", valueToLerp);
                stopped = true;
                valueToLerp = 0;
            }

            if (move == false)
            {
                playerAnimator.SetFloat("Forward", 0);
            }

            if (playerAnimator.GetFloat("Forward") == 0 && stopped)
            {
                move = false;
                rot = true;
                currPos = player.position;
                stopped = false;
                Debug.Log("Player current position when valueToLerp value is 0 : " + currPos);
            }
        }
        
        if(rot)
        {
            playerAnimator.Play("Walk Back");

            rot = false;
        }
    }

    private bool IsFacing(Transform target)
    {
        Vector3 forward = player.TransformDirection(Vector3.forward);
        Vector3 toTarget = target.position - player.position;
        return Vector3.Dot(forward, toTarget) > 0;
    }

    public IEnumerator ScaleOverSeconds(Vector3 scaleTo, Vector3 rotateTo, Vector3 moveTo, float seconds)
    {
        float elapsedTime = 0;
        Vector3 startingScale = player.localScale;
        Vector3 startingRotation = player.localEulerAngles;
        Vector3 startingPosition = player.localPosition;

        while (elapsedTime < seconds)
        {
            player.localScale = Vector3.Lerp(startingScale, scaleTo, (elapsedTime / seconds));
            player.localEulerAngles = Vector3.Lerp(startingRotation, rotateTo, (elapsedTime / seconds));
            player.localPosition = Vector3.Lerp(startingPosition, moveTo, (elapsedTime / seconds));

            elapsedTime += Time.deltaTime;

            yield return null;
        }
        player.localScale = scaleTo;
        player.localEulerAngles = rotateTo;
        player.localPosition = moveTo;
    }
}

In the Editor in the Animator controller, I have a transition between the state name "Walk Back" and "Idle" and it's working fine.

but in the script at this part, I start playing the "Walk Back" just after the player has stopped walking after changing the "Forward" speed slowly to 0 using Mathf.Lerp

if(rot)
        {
            playerAnimator.Play("Walk Back");

            rot = false;
        }

The problem is how to make a transition when the player has stopped and then smooth to start the "Walk Back"? Now it looks like it's cutting between the slowdown and stopping to the walk back and not smooth like a transition in the editor.

Screenshots of my animator controller :

Animator controller base layer with parameters

This is the blend tree Grounded after double click on it and the Forward is a float in the blend tree :

Grounded blend tree with the forward float

The Forward in the blend tree set the speed so when the player is walking I change in the script the "Forward" speed and that way control the player make it walk slowly or faster like slow down and increasing the speed slowly.

but I'm not sure how to use it with the state of the animation "Walk Back" ?

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Why play the animation directly with Play("Walk Back") when you could instead set parameters on the controller and let the controller blend it with the transition you authored in the state graph? \$\endgroup\$ – DMGregory May 4 at 15:21
  • \$\begingroup\$ @DMGregory I updated my question with two screenshots of my animator controller the "Forrward" parameter is controlling a Forward speed in the Grounded blend tree that way I set the player speed movement walking slowly down or slowly walking faster. but I'm not sure how to use it with the state of the animation "Walk Back" ? \$\endgroup\$ – Haim Hadad May 4 at 19:13
  • 1
    \$\begingroup\$ Usually you would do something like define a trigger for a transition and call animator.SetTrigger("TriggerName") to activate that transition. \$\endgroup\$ – Kevin May 4 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.