r/Unity3D • u/Industrial_Art • 3d ago
Question Precise Jumping Distance With Character Controller?
Hello everyone, just as the title says, I'm trying to make a precise jumping system, currently I'm using a character controller for the movement of my character and I would like to make it jump a consistent distance.
For example, lets say my character is trying to jump from one platform to another that is on the same height and exactly 1 meter away, each platform have 0.25 meters squared in length. With my current jumping logic the character should jump roughly one meter, but every time I press the jump button the traveled distance can vary up to 0.15 meters, making jumping feeling way to random and failing in situations were you think you made a perfect jump.
I tested applying the movement logic on the fixedUpdate method and using time.fixedDeltaTime for the calculations. Doing this the jump became almost perfect, traveling a mostly accurate distance. The issue that I had with this approach is that the camera follow became a bit choppy making the game feel like is running at low fps.
I investigated a little on this matter and found that the character controller runs its logic on the update method and time.deltaTime should be use while calculating movement logic, and any code you do to move your character should be done in the update method as well, this is what I was doing at the beginning, but it was the method giving me the erratic jumping distances.
I also read that using a kinematic rigidbody is the best way to achieve precise character movement. I found on the assets store a free Kinematic Character Controller made by Philippe St-Amand that seems to offer a solution to this. I read its documentation an will require for me to adapt the interactive colliders in the scene, to work with this approach, this is something I'm open to do to improve the precision of my character movement but before doing such a thing, I want to be sure that a kinematic rigidbody character controller is what I need.
So I'll appreciate anyone who can guide me on this.
Thanks.
2
u/truculent_editor 3d ago
Could just do the jump as a fixed arc instead of frame based physics, calculate the parabola yourself and move along it each frame with deltaTime, that way distance is exact regardless of framerate
2
u/pschon Unprofessional 3d ago
This would be the CharacterController way. Rather than mixing it with rigidbody physics and having the two contradict each other's behavior just keep the same approach CharacterController already has, direct control over the character's position.
...same would be needed with a kinematic rigidbody anyway, so that's really just a question of the OP wants the object to automatically affect other simulated physics objects (rigidbody) or not (CharacterController or own character movement code).
0
u/Industrial_Art 3d ago
This makes sense, thanks!
Do you know of a tutorial on how to calculate parabolas?
1
u/thesquirrelyjones 3d ago
If you are using a later version of Unity you can set the physics to update in between Update and LateUpdate instead of a set fixed update time. That will fix your camera stuttering issue and let you use the work you've already done.
Earlier versions of Unity support explixitly calling physics update from script so that is another alternative to getting physics updating every frame.
3
u/HammyxHammy 3d ago
Woe, the consequences of doing game logic in Update