Hi guys, I came across a vast array of problems with FPS Character Controllers, and had to scrap my entire script because of it. Now I’m working with a Rigidbody controller and it comes along pretty decent, except for the ladder climbing part.
Whenever my rigidbody character rotates their camera view while climing a ladder, they slide in the opposite direction. I think it is related to the rigidbody “rolling” against the wall surface.
There is a also a problem with remaining on the ladder, when the ladder moves. How do I keep my rigidbody attached to a ladder that slowly moves in space?
Did anybody figure out climbing a ladder with a rigidbody fps controller?
There’s a few ways to accomplish this. One way is to Destroy the rigidbody when you are on the ladder, and just have a ladder-traversal script that slides you up and down relative to the ladder’s position, which would also take care of the ladder moving. Then when you get off the ladder, add the Rigidbody back on, with any custom values it might be using.
Alternately, you could use a FixedJoint to attach to the ladder (ladder would need a rigidbody too obviously, one that is kinematic), and then drive the offset of the FixedJoint to go up and down the ladder. When you jump or fall off, just destroy the FixedJoint.
Assuming that you move a Rigidbody using AddForce during FixedUpdate, how would you get it’s new position to store it for future?
I’m currently doing the following ladder algorithm:
set climbing controller position to a precalculated pseudorelative to ladder value. This effectively lets the controller stay attached to the ladder no matter how it rotates or moves in space
move the controller in desired direction. Many other people have presented ladder scripts that put the player on rails while they are on the ladder. This approach, however, does not restrict player movement in any direction on the ladder, similar to the way it happens in most self-respecting modern fps.
calculate new controller pseudorelative position after the controller moves. This crucial step makes sure that the player is capable of moving on the ladder, and not just with the ladder.
This algorithm works well for me with non-rigidbody objects, but step 3 seems difficult to do with rigidbody movement. Without implementing step 3 I just have a rigidbody attached to the ladder.
The problem is - you can’t get the resulting position at the same FixedUpdate call, because at that point the position is not yet calculated. A regular transform moves immediately, and you can store its’ new position right after, but rigidbodies move during a separate internal physics update and there seems to be no way to hook into it.
I’ve tried obtaining the resulting position of Rigidbody movement using semaphores in a regular Update function like this:
Take note that the ladder game object needs to have a parent of some sort.
This script is derived from the work of whoever runs ThunderWire Games youtue channel. The main difference from it is that the controller is not just moving on the ladder, but also with the ladder, if the ladder itself is moving. An example of such a scenario would be if you have a boat that has a ladder on the side. The boat can move while the character climbs, and this script reflects that.
This is more of a proof of concept than a final form of this script, and you are free to correct/tweak it as much as you want. I know I will certainly change much of it myself, since right now the code is unorganized and sub-optimal.
Feel free to share your version of this if you come across any problems or if you just have a better solution.
I’d recommend making your character controller state-driven, this would make the code a lot easier to expand and maintain. It’s pretty hard to keep track on the changes on one big mess of conditional statements.
By doing this you could make a separate state for ladder movement which would specify how the character behaves and moves when he’s on the ladder.
I made tutorial about this recently on my blog that should explain how to do this, or you can just check the unitypackage and play around with it.
Your approach is interesting, I will deffinitely give it a go. I’m always eager to explore where patterns excell and fall flat.
In my defense the code I’ve posted is based on the standard assets fps rigidbody controller so that anyone can check it out and tweak it. People who go as far as to make their controllers state driven are far beyond the point where they struggle implementing ladder climbing.