# How to stop an object of going to far

look, I have this scripts, that detects where the cursor is, and teleport the object scope is (its basically to make the player aim in 2d with a sprite of a scope, its in my profile pic. The thing is, my script also detects the distance between the player and the scope,and i would like to make it so that the scope can go further than a certain distance for the player, so basically that distance is the range of the weapon

``````    scoop.position = Camera.main.ScreenToWorldPoint(new Vector3(
Input.mousePosition.x,
Input.mousePosition.y,
-Camera.main.transform.position.z
));
distance = (scoop.position - gameObject.transform.position).magnitude;
``````

(Ignore the fact that the player is a chicken)
Until now i have been “showing” the player the range with that circle, what i want to do actually is make the scope sorta rotate around the player if it gets to far, but make it so it can move frely if it isn´t too far.

In advance thanks for the answer, I love this community even if I barely know how to program

Very simple solution for you, as everything posted is overcomplicated:

``````        //calculate the target scope position as before, but store it in a variable
Vector3 targetScopePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,-Camera.main.transform.position.z));
//calculate vector from player to scope
Vector3 toScope = targetScopePosition - transform.position;
//limit the distance to the scope to the desired weapon range
toScope = Vector2.ClampMagnitude(toScope, weaponRange);
//use the limited vector to find the correct limited target position
targetScopePosition = transform.position + toScope;
//Set the scope to the target position, but keep its z position so it doesnt get lost behind anything.
scoop.position = new Vector3(targetScopePosition.x, targetScopePosition.y, scoop.position.z);
``````

Assuming you already have your scope in your “scoop” variable and stored the distance limit in “weaponRange”

I had a go at the maths for you to try, I’m not that fantastic at coding but I think this should be close.

``````Vector2 ScoopVector;            // World position of the Scoop
Vector2 ChickenVector;          // World position of the chicken

float angle;                    // maths
float realativeX;               // more maths
float relativeY;                // much more maths
float distance;
float weaponRange = 1f;

void Update()
{
// Work out the world position of the mouse (put into scoop vector)
ScoopVector = new Vector2(
Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
Camera.main.ScreenToWorldPoint(Input.mousePosition).y
);

// Work out the world position of the chicken
ChickenVector = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y);

// Calculate the distance between them
distance = (ScoopVector - ChickenVector).magnitude;

// If the distance is too far, change the x and y values
if (distance > weaponRange)
{
// The maths
realativeX = (ScoopVector.x - transform.position.x);
relativeY = (ScoopVector.y - transform.position.y);
angle = Mathf.Atan2(realativeX, relativeY);

// New x/y coordinates
ScoopVector.x = weaponRange * Mathf.Sin(angle);
ScoopVector.y = weaponRange * Mathf.Cos(angle);

// Finally apply the position offset from your Chicken
scoop.GetComponent<Transform>().position = ScoopVector + ChickenVector;
}
else
{
// Apply the position (no adjustment needed)
scoop.GetComponent<Transform>().position = ScoopVector;
}
}
``````

Incase this doesnt help, I was thinking of a vague idea you could maybe try, using ‘LookAt’.
If the scoop was a child of the chicken, and you used ‘lookAt’ to rotate it with the mouse
The local axis of the scoop should move with the rotation: Then all you need is the distance value you already made to set a local y axis offset
some thing like:

``````if (distance <= Range){

localOffset.y = distance;

}
else{

localOffset.y = Range;

}
``````