+2 votes

I'm already aware on how to create a wall using collision to prevent player from overlapping an object.

Now I wan't is to prevent the player from moving outside the box or circle or any shape but I don't know if this is possible. Or the only way to do this is create a "C" shape to act as a collider wall?

in Engine by (27 points)

1 Answer

+2 votes
Best answer

Use a CollisionPolygon2D and set the Build Mode to Segments.

It's easy to build a box shape inside the editor. But if you want a circle or any shape:

  • Create a Sprite with a texture of that shape (an image of a circle for example)
  • Press Sprite > Create CollisionPolygon2D Sibling

Images from Github

by (4,169 points)
selected by

Hi, Thanks for replying!
I tried your suggestion but this does not prevent my player from going outside the polygon I created. Do I also need to attach some scripts?
I want my player movement to be contained only inside the polygon like trapping him inside an invisible bubble.

No scripts needed. I think you might have the wrong settings. Do a double check.

Proof that it works:

Proof

Thanks for the pic.
I didn't know I should use StaticBody2D. (I tried using Node2D as parent)
It works now as expected.
Thank you!

This works like a charm, I also found that going into the generated PoolVector2Array under the Polygon type one can easily edit the coordinates to make pixel perfect rectangular bounds.

There might be an easier way to create a perfect rectangle and segment it but this approach worked well and took all of 5 minutes to create two separate zones to be used to separate different pong paddles.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.