XaiJu
Memora Wanderer
Memora Wanderer

patreon


The Camera Collider

Last night I remade the camera collider. Before it used to smoothly move through collisions, now it's much snappier. It can now also go into 'first person mode'.

For those interested in the more technical side of things, read on!

Before it used to cast a 'ray' from the player to the camera (a single point in 3D space moved in a line, like a ray of light). If there was something in the way, it'd reduce it's target distance until it wasn't colliding anymore, and smoothly move the camera towards that target distance. That mostly worked, but while it kept the camera smooth, a bit part of the feedback was that players could see through walls and floors at times.

Changing it to snap with the raycast worked slightly, but caused the player to fling outside of the environment! Because the player sprite's angle changes to face the camera, and the player's collider adjusts depending on how angled they are to the ground (so the player's head doesn't clip through walls). The sudden changes to a close up view was causing the player's collision object to move to a new position instantly... through a wall.

That's where the 'first person mode' comes in. I hide the sprite and don't adjust the player's collider anymore when the camera is too close. Moving around in first person at times looks kinda cool, too.

There were some other things bothering me about the raycast. A raycast is just a line, there's no buffer zone for it being 'near' a collision object. So I swapped it to use a SphereCast3D node. The sphere produces a spherical collision detection area and then moves it in a target direction. Simply put, if the user wants the camera zoom to be at '20', the sphere starts at the player and then moves 20 units in the direction of the camera. If at any point the sphere collides with something, the middle of the sphere is where I move my camera.


More Creators