How to make a FPS controller climb a wall?

Hi,

i’m currently trying to build a fps with custom physics (in order to make the character fall on slopes and behave more realistically than the standard fps prefab) and after making it walk, run and jump i’m currently looking for making it climb walls…

I made a search on this forum and generally on the net and i didn’t find anything good to start with… One interesting post was one regarding ladder climbing (http://forum.unity3d.com/viewtopic.php?t=14029&highlight=climbing) which could be used as a starting example in order to have climbable zones (to be set by hand like in old tomb raider if i’m not wrong)…

However this wold be really annoying in my opinion… isn’t there a smarter solution? Can somebody suggest me the best approach to adopt?

Thanks…

Nobody has neither an idea nor a suggestion on how to start dealing with this problem? I don’t believe that anybody hasn’t ever had to deal with it…

Please…

I suppose ideally the climbing can be done with the forward / backward buttons instead of using a different key. Maybe with a ‘climbing’ boolean variable, on the character controller, which is set to true when the circumstances are right, and to false immediately upon them being wrong. This variable when false allows forward and backward movement, and when true adjusts the movement vertically, in a manner you design to fit climbing. Some games have a smooth steady vertical movement. Others have brief bursts of movement designed to simulate the character being pulled up in steps.

This ‘climbing’ or ‘isclimbing’ variable can be set to true on collision with certain objects (perhaps only if they are also facing those objects, or after a certain amount of time running into the wall). Using tags you can designate whether or not a given wall will allow climbing, checking the tag in the script before adjusting the variable. Once it is true, you may want it to turn false as soon as the colliders are a certain distance apart.

There’s likely a better way to do it, but that’s what comes to mind

Hi Alec, first of all thank you for the answer. However, i don’t understand clearly if your talking only of the ladder example or more in general regarding climbing…

For what concerns climbing ladders, the example project posted above is almost a good starting point, however there are several bugs which i’ve already fixed (going over the top most trigger, remaining in the center of the ladder, etc). The fact of having a key for climbing is not a bad idea in my opinion, instead the one for stepping off the ladder is not a good idea, but for this is not a big deal… I can allow the climbing on the ladder with the jump key and the stepping off when the other trigger is entered…

But the ladder (which works also for ropes) if fine now… The point is that climbing walls or other objects is not as climbing ladders…

My current idea (not implemented 'till now) is the following:

  • Having a box collider as a child of the fps controller, right where the hands should be while jumping

  • Placing a box collider over the walls which the player can climb with an attached script for bookkeeping variables like the one in the project above

  • If there is a collision (when the player jumps against the wall), lock the fps to that wall (like for the ladder) and allow right/left movements (along the wall).

  • If the player jumps again (or whatever other way, also forward key pressed) let the fps controller climb on top of the wall (as in the project the controller goes over the upper platform)

  • If the player presses another key (for releasing) the will be de-attached from the wall and he will fall back on the floor.

[/list:o:1ae95590a5]
Somehow this is the idea that i have in my mind for solving this situation… The point is: is this idea stupid?
The drawback is that one should spend a lot of time in placing the climbable gameobjects around the scene, but i can’t figure out another solution.

Certainly not a stupid idea.

The methods of your solution sound great; jump-on, release-off, climb up.
All the things you should have while climbing. It seems like it might be overcomplicating things, though. I’m no expert, so I could be missing some important things about game physics, but here’s my thoughts:

If the hands are not going to be visible, it may not be all that necessary to accurately predict their location when jumping to climb, though if you meant estimating their total potential positions (like a large sphere collider), I could certainly see the benefit of that.

If you have a lot of climbing areas, scripts may be a waste of system resources.
I may be being naive or idealistic, but I don’t see why the climbing object will need a script of its own, unless the climbing methods themselves must be unique to the object, and even then tags would probably be faster and easier.

Regarding the areas of climbing, I would make a ladder-sized box collider on an empty game object, give it top and bottom zone colliders as children, and make that a climb_section prefab. This prefab would be stretched wide for walls, you can adjust the top and bottom as needed, and would initialize climbing in the character controller whether stepped up to (bottom collider) or jumped on to (any collider). This makes the climbing areas quick to set up, and adaptable to changes in the scene’s setup. if you tag them all, you can turn them all off to prevent climbing, say if the character was carrying a big or heavy item. You can check for the colliders name, and if its bottom zone and the character’s already climbing, stop climbing. Otherwise, start. (you’d have to check for the initial collision for this, not every frame they’re collided).

In their basic function, there’s no reason that jumping to a ladder should be any different from jumping to a climb-allowed wall, except for movement left or right. With a generic climb section that can be resized, that is solved.
Any specific differences in climbing one object versus another can be solved with giving them different tags, and having the character script respond to those tags differently.

Again, sorry if I’m not thinking this through all the way, or if I’m not helping.

First time I ever heard about a state machine I fell in love with it and I use it extensively in my Unity projects combined with enums to make my states more readable…

For example, a movement state of normal, swimming, climbing etc etc and in the Update function add a Switch() that calls appropriate movement code split into separate functions.

Hense, all standard movement code goes in function normalMove(). When the user collides with a climbable object, parent the player to the object, turn off gravity and call function climbMove(). This function tests the vertical and horizontal buttons and moves appropriately. When the player pressed release, simply un-parent the player, turn gravity back on and change the state to normal… Problem solved…

This makes for much easier code to read in that each different mode uses the keys in a different way and each mode only tests for the available buttons while in that mode. For example, the zoom key will be of no use while climbing a wall, so in the climbMove() function, simply don’t make provision for what happens when the user hits button Z…

enum eMoveState {normal = 0, swim = 1, climb}
static var moveState : eMoveState =eMoeState.normal;
var moveSpeed : float = 50;

function Update()
{
   switch (moveState)
   {
     case eMoveState.normal: normalMove(); break;
     case eMoveState.swim: swimMove(); break;
     case eMoveState.climb: climbMove(); break;
   }
}

function swimMove()
{
   var hor : float = Input.GetAxis("Horizontal");
   var dist : float = movespeed * Time.deltaTime;
   var ver : float = Input.GetAxis("Vertical");
   var amt :Vector3 = Vector3.zero;

   if (hor != 0)
   {
      if (hor < 0)
               amt.x = dist * -1;
      else  amt.x = dist;
   }

   if (ver != 0)
   {
      if (ver < 0)
               amt.y = dist * -1;
      else  amt.y = dist;
   }

   transform.Translate(amt);
}

Untested, but you should get the idea…

Wow, thanks guys…

@Alec:
The fact of having a collider on the player to simulate the hands is just for being more realistic and allowing the climbing only when a specific part of the player enters a climbable volume…

For what concerns having a unique prefab for climbing (ladders, walls, ropes, etc), I think it could be a great idea! Having the different climbable objects tagged differently do allow a check of the type of the object and a generalized code for handling the behavior of the player… I think i’ll work on this idea

@MrDude:
State machines… Finite Automata… Things I’ve hated during university lessons… :wink: I’m joking… Things of great potential for sure.

Your approach however seems more imperative than logical (in the theory of computing sense of the term, :-)) in the sense that you’ve the control, not the machine nor the states. It’s something that i’ve always done in that king of courses, and that was the answer i’d alway get, so don’t thing i’m making fun of your idea… On the contrary it is simply great!
Thank you a lot, I’m not 100% comfortable 'till now with the coding in unity (due to other implementing strategies that i had to adopt in other projects), so i was’t seeing the nice and clear solution that you’ve proposed…


Let me recap a bit and merge somehow the ideas to see if i get everything:

  • Create a general climbable prefab with sensible areas which could be used for several climbable objects;

  • Tag the different climbable gameobjects in different ways;

  • Create an enum state structure to keep the various possible states that the character could perform (walk, climb, etc)

  • On the fps controller script have a OnCollisionEnter function which checks for collisions with sensible areas (the one of the climbable prefab) and:

  • Changes the enum state structure accordingly

  • If there is a collision with a climbable object parent the player to that gameobject [is this really needed??]

  • Turn off gravity

  • On FixedUpdate (since i don’t use the fps prefab of unity but i have my custom physics for moving the player [right??]) check with a switch the current state and call the appropriate movement function with the specific code for the movement to execute

  • Execute the movement function which will allow or not several movements

  • I will also insert a OnCollisionExit function (also done by pressing the “release” key which will be checked on Update [or is it better on FixedUpdate??]) which:

  • Restores the normal movement state

  • Restores gravity

  • Detaches the player from the gameobject (Note: All of this only if the player isn’t in normal state)

[/list:o:68cb585b1d]

I think that’s almost all, am I right? I would really thank both of you guys for the suggestions. If you want to help me in solving the few open questions that i left on the list of steps that i’ve sketched it would be simply great…

That looks about right. The general climbable prefab can be adjusted on a case-by-case basis.

You might find that you don’t need many tags. You can probably get by with having one default climbable tag on the prefab, which you might change to a new tag for special cases.

Parenting the object isn’t exactly needed, but it is recommended if you ever want to have dynamic surfaces, like ropes or ladders that move. This will move the player with the object automatically. If you prepare for those situations in your script now, you won’t have to work it into it later.

Looks like you’re onto the right track

Thanks Alec…

However I think that having different tags for the different type of climbable object that is encountered will help in the modification of the current state the player should be in, or?
For what concerns the parenting, i’ve thought about it and probably is the best solution if i look a bit forward when possible changes to some behaviors may be requested…

I noticed also a small mistake in my list of the previous post: point 7. That portion of solution should be executed by the “release” button, but not OnCollisionExit, but rather OnCollisionEnter of the other trigger of the climbable object… If not the player would be detached as soon as he exit the first starting trigger…

If you have ideas to modify the depth of climbing with many tags, I definitely would encourage you to explore them. I’m sure that multiple tags will add plenty of options and certainly make various climbing states easy to manage.

Can any one make a tuturial how to make this work… Im a noob just started…