# Limit the radius of movement of the player

Hi.
I am trying to implement a restriction on movement by a player in a certain radius. How to prevent the player to go beyond the radius of the object? At the same time without restricting its movement within the region.

``````if (isLocomotingRight) {
// Player current position
FVector CurrentPosition(this->RootComponent->GetComponentLocation());
// The coordinates of the object around which we will walk
FVector AnchorPosition(2091.0f, -11052.0f, 0.0f);
// Calculate the distance to the object
FVector vector = AnchorPosition - CurrentPosition;
float AnchorDistance = vector.Size();
float maxDistance = 220.0f;

/*Here I need some code to impose a value on offsetDir*/

// Next comes the calculation of the displacement
FVector offsetDir;
offsetDir = locomotionDirectionRightX * locomotionRightXMultiplier + locomotionDirectionRightY * locomotionRightYMultiplier;
offsetDir.Z = 0;
if (isLocomotionSpeedConstant) offsetDir.Normalize();
}
``````

Is there an elegant way to do this?

i would do something like that, but that could put some jitter back and forth. you could play with the dot product of offsetDir, -vector and if > 0 set offsetDir to 0 ?

``````FVector offsetDir;
offsetDir = locomotionDirectionRightX * locomotionRightXMultiplier + locomotionDirectionRightY * locomotionRightYMultiplier;

if(AnchorDistance>maxDistance)
{
offsetDir+= vector.GetSafeNormal() * 2*offsetDir.Size(); // we go 2 time faster than current input back to start point ;
}
offsetDir.Z = 0;``````

You donâ€™t need to code it, you can use mesh as a walks with a material with 0 opacity and collision

Yes, initially I also did a check, but with an indication of each axis separately.
offsetDir.X Y Z
And it was awful) Your option is more elegant. Jitter back is not critical. Thanks a lot for the function.

Thank you, but I wanted to resolve this without collisions.