# How can I build a virtual barrier to block and prevent from the player to keep moving forward?

This is my logic :

When the player is moving I'm getting the distance from another object.

When the player is at distance for example 61 slow down the player moving "Forward" until the player stop and can't move any further forward.

The same logic should be applied for any direction the player is moving from the object not only from the object front or the sides 360 degrees no matter where the player is moving to when the distance is 61 start slow down the player to stop.

The next logic/rule is one of my biggest problem :

When the player is rotating by me and facing the other direction the give the player to option to move again this time make the player start moving again from slowdown to the regular walking speed. but only if the player is facing the other direction.

Now the problems I'm facing :

When the player moved forward reached distance 61 then start slow down and then stop, if the player is rotating facing the other direction and start moving back even a bit for a millisecond then if I rotate the player to the forward direction again he can move again and pass the distance 61. This way even if the player stopped already I can cheat the block by rotating facing the player the other direction moving only a bit then rotating back again and move much more. I need some how to make that if the player stopped then rotating the other direction start walking again only if he reach some distance like 50 or 40 then he can rotate back and move forward again and slow down and stop at distance 61.

The second problem is when I'm moving far away from the object but in another place from the side or from the back then the player is walking/running automatic and everything get messed. The play not stopping not slow down.

I can record a very short and small video clip to show what I mean, What it's doing so far and what are the problems if the video clip maybe will be more help to understand what I'm trying to do.

This is a short video clip I recorded showing the problem/s.

The problem/s I shot it starting from second 37 : in this case the distance is checked from the big space ship behind 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;

// 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)
{
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)
{
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 (distance > 61f && Mathf.Abs(anim.GetFloat("Forward")) < 0.003943384f)
{
anim.SetBool("Idle", true);
}
else
{
anim.SetBool("Idle", false);
}
}
}