• News

• Industries

• Learning & Support

• Community

• Marketplace

# Cylinder Gravity or Attraction

Hello forum,

I’m looking to make a 2.5D game with a fixed camera on a cylinder which will appear as a circle with layers like a planet cross-section. The ACharacter will run around the outside of the circle 360° and try to catch incoming missiles. Wedges of a layer will be destroyed by the missed missiles and the ACharacter will be able to fall down a layer if he runs over a hole.

I’ve done some googling but come up with only pretty complicated and poorly documented solutions to add gravity at the centre of an object (or at the centre of the screen in my case). An example of the behaviour I’m trying to replicate can be seen HERE.

I want to ask the forum, does any particular solution come to mind? Any tricks that would achieve what I’m looking for?

Cheers.

Since it sounds like you don’t need a full fledged gravity system, here’s an idea that may work for you.

Each frame, run a ray trace from your character towards the center of your cylinder. When the ray trace intersects one of your layers, place your character so their feet are at the point of intersection. Also, be sure to rotate your character so it looks like they are standing on the surface of the intersected circle.

If the ray trace doesn’t intersect one of your circles, be sure to handle it properly. That’s a situation where, based on your game play description, they would end up falling all the way to the center of the cylinder.

Thanks, @Twig314159 ,

Do you think your technique could handle jumping as well?

Yes, I was thinking about that too. It should be able to handle jumping. I don’t know all the math you’d need off the top of my head, but a here’s a general concept.

Anytime your character is jumping, you would want to set a bool, like “IsJumping”. As long as this bool is true, don’t place the character on the surface of your cylinders, instead place them in the air. The point in the air where you place your character depends entirely on how high you want them to jump and how quickly they should be pulled back towards the surface of your cylinders. You’ll still want to rotate the character even while in the air so it looks like their feet still point towards the center of the “planet”.

For jumping you’ll likely have to do your own math to launch the character in the air and make them fall back towards the center. It shouldn’t be too hard, but unfortunately I’d need to spend some time thinking about the math and trying it out. In general you’d want some force to launch them away from the center (the jump action) and then some gravity to start pulling them back towards the center. Do the correct math (this is the part I’d have to think about) and it should give you a position to place the character until they collide with your cylinders again.

You should be able to use Unreal’s collision system to detect when they’ve made contact with your cylinders as they fall. Once you detect contact, you’d set “IsJumping” to false, then the ray trace would take over again and make sure they stay on the surface.