How can I create a walking zone logic for my player?

First I will show a screenshot and explain the logic I think of and what I’m trying to do :

Diagram

The small circle is the Player.

The black rectangle is the target the player is walking towards.

The green zone is where the player slow down. Once entering the green zone and facing the target by calculating the distance from the target the player start slowing down until stop.

When the player just rotating facing the opposite direction away from the target in this point something show switch the speed logic if the player was slow down to stop if the player is facing the opposite direction he can start walking again and the speed should be increasing slowly to the normal speed the same idea when slowing down to stop so speed up to normal.

Now if the player is leaving the green zone once left the green zone using a flag or some helper and reset everything so if the player will enter the green zone again facing the target he will slow down to stop again.

  • When the player is getting some distance from the target and facing the target slow down to stop.

  • When the player stopped and then if the player rotating facing the opposite direction let the player the option to move again with increasing the speed back to normal.

  • When the player is moving away from the target some distance for example out the green zone reset everything and if the player will face the target again and enter the green zone as distance from target slow down to stop again.

This script is attached to some object and make the logic I wanted :

using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine;  public class FireEffect : MonoBehaviour {     public GameObject player;     public GameObject longDistanceDescriptionTextImage;     public TextMeshProUGUI text;      private Animator playerAnimator;     private float timeElapsed = 0;     private float lerpDuration = 3;     private float startValue = 1;     private float endValue = 0;     private float valueToLerp = 0;     private float angle;      // Start is called before the first frame update     void Start()     {         angle = transform.eulerAngles.y;         playerAnimator = player.GetComponent<Animator>();     }      // Update is called once per frame     void Update()     {         var distance = Vector3.Distance(player.transform.position, transform.position);          Vector3 dir = (transform.position - player.transform.position).normalized;         float dot = Vector3.Dot(dir, player.transform.forward);          angle = transform.eulerAngles.y;         if (distance < 10 && dot > 0.9f)         {             if (timeElapsed < lerpDuration)             {                 valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);                 playerAnimator.SetFloat("Forward", valueToLerp);                 timeElapsed += Time.deltaTime;             }             playerAnimator.SetFloat("Forward", valueToLerp);             longDistanceDescriptionTextImage.SetActive(true);             text.text = "It's too hot , I can't continue.";         }         else         {             if (dot < -0.9f)             {                 if (timeElapsed > lerpDuration)                 {                     valueToLerp = Mathf.Lerp(endValue, startValue, timeElapsed / lerpDuration);                     playerAnimator.SetFloat("Forward", valueToLerp);                     timeElapsed += Time.deltaTime;                 }                 playerAnimator.SetFloat("Forward", valueToLerp);             }         }     } } 

I’m not sure if this is the right way to make the script but it does working for the slow down to stop part :

if (distance < 10 && dot > 0.9f)         {             if (timeElapsed < lerpDuration)             {                 valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);                 playerAnimator.SetFloat("Forward", valueToLerp);                 timeElapsed += Time.deltaTime;             }             playerAnimator.SetFloat("Forward", valueToLerp);             longDistanceDescriptionTextImage.SetActive(true);             text.text = "It's too hot , I can't continue.";         } 

but it’s not working in the else part the speed is not increasing slowly back to normal but increasing at once to normal. The lerp is not working for the increasing back.

another problem is when the player in the else part facing away from the target if I will rotate the player with the keys and move him again to the target he will walk regular nonstop to the target without slow down again.

I’m not sure what logic should be here for 100% but I think if the player is facing away from the target start increasing to normal speed and if in the middle I rotate the player facing the target again because I’m inside the green zone then slow down to stop the player maybe a bit faster to keep the same 10 distance like in the script from the target. Don’t let the player to pass over the 10 distance from the target when facing the target.