unity – I can’t figure out why the bool flags are not working in the right order?

This script is attached to a wall :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WallCollider : MonoBehaviour
{
    public static bool entered = false;

    private void OnTriggerEnter(Collider other)
    {
        entered = true;
    }

    private void OnTriggerExit(Collider other)
    {
        entered = false;
    }
}

When the player get enter the flag is true when exit false.

This script is attached to the player :

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;

public class DistanceCheck : MonoBehaviour
{
    public GameObject distanceTarget;
    public GameObject longDistanceDescriptionTextImage;
    public TextMeshProUGUI text;

    private Animator anim;

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

    // Opposite Direction
    float timeElapsedOpposite = 0;
    float lerpDurationOpposite = 3;
    float startValueForOpposite = 0;
    float endValueForOpposite = 1;
    float valueToLerpOpposite = 0;

    float angle;
    bool exited = true;

    // Start is called before the first frame update
    void Start()
    {
        angle = transform.eulerAngles.y;
        anim = transform.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        var distance = Vector3.Distance(transform.position, distanceTarget.transform.position);
        angle = transform.eulerAngles.y;
        if (distance > 61f && angle < 180 && exited == true)
        {
                if (timeElapsed < lerpDuration)
            {
                valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
                anim.SetFloat("Forward", valueToLerp);
                timeElapsed += Time.deltaTime;
            }
            anim.SetFloat("Forward", valueToLerp);
            timeElapsedOpposite = 0;
            longDistanceDescriptionTextImage.SetActive(true);
            text.text = "I can't move that far by foot. I need to find some transportation to move any further.";
        }

        // Get the angle:

        if (angle > 180f && distance > 61f)
        {
            exited = false;
            text.text = "";
            longDistanceDescriptionTextImage.SetActive(false);

            if (timeElapsedOpposite < lerpDurationOpposite)
            {
                valueToLerpOpposite = Mathf.Lerp(startValueForOpposite, endValueForOpposite, timeElapsedOpposite / lerpDurationOpposite);
                anim.SetFloat("Forward", valueToLerpOpposite);
                timeElapsedOpposite += Time.deltaTime;
            }
            anim.SetFloat("Forward", valueToLerpOpposite);

            timeElapsed = 0;
        }

        if(WallCollider.entered == false)
        {
            exited = true;
        }

        if(WallCollider.entered)
        {
            exited = false;
        }
    }
}

And in this script I can’t make the logic I wanted.

The logic should be very simple :

  • When the player is at distance more then 61 and at angle less then 180 that’s meaning the player is at distance 67 or more and facing the wall.

  • At this point the player start slowing down until stop.

  • Now, when the player is rotating and the distance is still more then 61 but this time the angle is more then 180 that’s meaning the player is not facing the wall anymore. And the player can start walking again this time the player walking speed increasing slowly until normal.

  • When the player exit the collider/s area everything should be reset and then if the player enter the collider/s area again and in distance more then 61 he should slow down again.

The problems I’m facing :

  • If the player is at facing the wall(collider/s) then slow down and stop if I turn the player to the right or to the left and move him he start walking and never stop walking even if I’m not pressing any of the keys WSAD. If I rotate the player directly opposite by 180 degrees then he will slowly increase the speed will walk and I can control it again. but for some reason if the player after stopped rotate to one of the sides and walk he keep walking nonstop automatic by his own.

  • When the player at distance more then 61 slow down and stopped if I rotate the player by 180 degrees and start walking again with the keys before the player increased to full speed to normal speed if I rotate the player again by 180 and make it walk the player will slow down and stop but much further after the last stopped position. This way I can rotate the player many time and make him move and move much beyond the distance 61. And I want that the limit distance the player can go each time will be the same 61 and from there to slow down and stop. And only if the player exited the collider area and then rotate again and move inside the collider area and slow down and then stop again.

  • Now if the player is inside the collider area he can rotate many time and keep moving more then 61 distance the player rotate move and slow down rotate back and then rotate again move a bit and slow down to stop this way you can do it nonstop times and the player will get much far away.