Home > front end >  keep the gameobject inside the circle and not exceed the circle
keep the gameobject inside the circle and not exceed the circle

Time:04-22

How to keep the gameobject(Red ball) inside the circle and not exceed the circle. enter image description here

CodePudding user response:

Maybe you can try this, to keep the Red inside the circle.

float radius = 100; //radius of white circle
Vector3 whiteCenter = circle.transform.position; //center of white circle
float distance = Vector3.Distance(Red.transform.position, whitePosition);

if (distance > radius){
Red.transform.position = whiteCenter;
}

and if you want to limit movement in a circle

if (distance > radius){
    Vector3 fromOrigintoObject = player.position - whiteCenter;
    fromOrigintoObject *= radius / dist;
    player.position = whiteCenter   fromOrigintoObject;
    Red.transform.position = player.position;
}

  • Related