- Home /
[Please Help] 2D keeping an object within a fixed radius from main object
Hello.
Im trying to make a certain sprite to follow mouse position but stay within a fixed radius from the main player sprite, so it will look like it only circles around the player. i need to only lock the distance of from another without touching rotation of the object as i need them to stay in the same rotation. the game isn't a clean 2D axis game, I am using the z and x axis.
void StepRotateMovement()
{
lockedDistance = Vector3.Distance(shadow.transform.position, transform.position);
shadow.transform.position = convertedMousePosition;
if (lockedDistance != 3f)
{
Vector3 fromOriginToObject = shadow.transform.position - transform.position;
fromOriginToObject *= 3f / lockedDistance;
shadow.transform.position = transform.position + fromOriginToObject;
}
}
This is what I tried doing but as of result i am getting the shadow object to glitch infinitely between the center and its new position. besides that it doesn't lock its distance and instead it only reduces it.
Thank you in advance!
Your answer
Follow this Question
Related Questions
Movement on prefab objects 0 Answers
Should i use terrain generation or make it myself for android? 0 Answers
Jump to position in front of an object 1 Answer
Keep vectors exactly 15 distance apart 2 Answers