I’m prototyping a game on rails and have set the character to begin at a set speed and accelerate to a maximum speed, translating forward in a set direction.
Now I want to add pits that the player must jump over, so I created an input event which plays a simple jump animation on the character to which the main camera is a child, but realized I need to force the player to translate forward so the jump covers the gap.
Is this the best method to achieve the simulated “leap” that I am looking for? I have seen examples where the player is a rigid body and physic’s is used to drive him through the level, applying momentum when keys are pressed, but I was hoping to keep things simple by creating a simpler approach since the ‘wasd’ keys will trigger animations on the character mesh to avoid trigger checks with obstacles rather then deal with physics.
I am a very novice coder and would appreciate any advice from more experienced developers.
You need a way for the jump translation to keep occurring while the player is in the air. As your code stands now the player would only move forward while the jump button is being held down. It’s slightly more complicated than it sounds to determine if the player is grounded or not… luckily there are built-in scripts which are part of the Standard Packages which do this for you (if you use a CharacterController and the CharacterMotor script for example).
The only problem is these scripts are not meant to work with a “rails” style game, but you might find a way of integrating that into it. For example, if you have the standard CharacterController, CharacterMotor, and an input controller, you would just need to somehow constrain your character’s movement to the rails. This could be as simple as restricting your character’s movement to it’s local YZ axes, and restricting it’s rotation to always face forward on the path.
It sounds complicated, but it would probably be easier than trying to handle all the physics required for jumping, falling, sliding etc. that might occur even in a “rails” game.
After consulting a friend who coded a rail shooter, he suggested I ditch the physics for pre-set translations and animations to avoid the unnecessary complexity of such a system. Since I know the set distance a player will translate when they jump (and jump is a triggered animation so once they hit up, the jump anim plays and they translate forward so they clear the pit they’re jumping over), there really isn’t a way to stop jumping till the animation completes – which is good, since that would break the jump otherwise.
What I’m doing is setting an inital speed, then ramping it up (to provide an inital acceleration) and also make the game harder to dodge as you gain speed. Ultimately I will reduce the speed when you strike a blocker (this is also why the fixed translate distance when you jump is handy, so you don’t have to pick up speed to clear a gap.)
I suppose this method of if statements is fine, but I might make it a switch instead just for clarity. Right now the dodge, slide and jump anims work well on the character to avoid trigger events with the blockers while the driver keeps pushing the character through along the current speed.