I am fairly new to coding in game engines, but have some mild programming experience outside of them.
I'm making a 2D game with 3D characters, so I am using the 3D environment minus the Z axis. For the life of me, I cannot figure out how to get the character to interact with the floor (especially slopes) in the way that I want it to. Code is being written in C#
My desired effect is as follows: The character should follow the contour of my 2D floor, as if they were riding a roller coaster across the curve. The character should not leave the floor until a jump command is explicitly entered, no matter the angle or speed of approach.
(my beautiful MSPaint artwork)
example:
Currently, I am using the MoveAndSlide()
function with ApplyFloorSnap()
. When I enter the jump command, I set the FloorSnapLength
to 0, otherwise, I have it set to 0.25. The game will have very high movement speed, which is why I have the FloorSnapLength
set so high. This causes artifacts in movement. Examples include snapping to the ground at a visible distance, odd falling into slopes, and snapping to the floor while jumping. This happens while still allowing unwanted behavior, for example entering then quickly exiting a slope will pop the character into the air. A video example is below, in a google drive link. Setting the FloorSnapLength
any lower causes far more air popping off of slopes. I attempted to use the MoveAndCollide()
function, but there is next to no documentation on how to use it effectively, and having any sort of gravity causes the player to get "stuck" to the floor, my guess is as a result of the constant collision return setting velocity to 0. In order to remedy this, I tried two methods. First, I tried removing gravity while on the ground. This did not give the desired effect on slopes, as going down a slope would cause the player to detach from the ground. Second, (with gravity on all the time) I tried brute-forcing movement by updating the players position directly when MoveAndCollide()
returned a collision with the floor. I did this using GlobalPosition = new Vector3D()
, and inserting the velocity on each axis * delta. This worked better, but is bad practice and updated the Y component of position far less often than it should have, for whatever reason.
examples:
Video example
If possible, I would vastly prefer to use MoveAndCollide()
, as it gives me far more precise control over how the player moves, which is incredibly important in the game that I am creating. I have looked at the code for MoveAndSlide()
in the GitHub repository in an attempt to emulate its basic ground movement, but I can't make much out of it.