I've been trying to add boundaries to my space game.

I've been following this tutorial with a sphere instead of a cube, but every time I start the game and move or rotate my ship it appears outside the sphere, as shown in the image: OutOfBounds

The ship has a collider and a characterController.

Could anybody tell me what I'm doing wrong here? Thanks!

  • \$\begingroup\$ It looks like you've misunderstood the tutorial. The tutorial was not showing you how to create a space the player cannot leave, but how to create a space the player cannot enter. They used this non-enterable space as a wall to "plug" an open edge od their map that the player could otherwise walk off of. \$\endgroup\$ – DMGregory Sep 27 '19 at 17:02
  • \$\begingroup\$ I see, I misunderstood then. I already have an answer as how to restrict the movement when you are further from a radius. But out of curiosity, there is any way in Unity that would allow an object with collider to be inside another and still have collision with the outer one? \$\endgroup\$ – Hector Sep 28 '19 at 17:30

sphere restriction all means a distance from center point.

Vector3 center = new Vector3(0, 0, 0);
float thresholdDistance = 200;
private void Update()
    if (Vector3.Distance(transform.position, center) > thresholdDistance)

this code make the player to return to sphere zone when is further than a distance from center.

  • \$\begingroup\$ Interesting, at first I was thinking more in using an impassable sphere because maybe It could be more visual when arranging the scene in Unity, but honestly this way seems better. Perhaps I might use another way to show the player that further travel in that direction is forbidden, like a message or just stopping the movement. But I like It, thank you! \$\endgroup\$ – Hector Sep 28 '19 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.