Search Unity

I need help trying to get a wall jump script working

Discussion in 'Scripting' started by DatGameDesigner, Mar 16, 2012.

  1. DatGameDesigner

    DatGameDesigner

    Joined:
    Mar 2, 2012
    Posts:
    6
    So I'm currently using the Platform Controller script found in the 2D Platformer tutorial: http://unity3d.com/support/resources/tutorials/2d-gameplay-tutorial

    I'm trying to add a wall jump ability to it, but so far things haven't been working out. The wall jump mechanic I want to add is more similar to what you would find in a megaman X or super meat boy game where jumping while on a wall sends you outward and then upward and allows you to choose if you want to fall towards that same wall or shift to the other wall.


    only things I've added so far is

    var canWallJump : boolean;


    if(hit.gameObject.tag == "Wall" controller.isGrounded == false)

    {
    canWallJump = true;
    if(Input.GetButtonDown ("Jump") canWallJump)
    {
    movement.verticalSpeed = CalculateJumpVerticalSpeed (jump.height)
    //canWallJump = false;
    Debug.Log("ss;aljf");
    }
    }

    all this does currently is allow the player to jump again when he touches the wall but it only results in him sliding up the wall.
     
  2. bajeo

    bajeo

    Joined:
    Dec 5, 2011
    Posts:
    75
    Hey,

    Havent done this before but i think you have a good starting point.

    I think you want to keep a record of the velocity from before you hit the wall (and it becomes 0) so you can convert this forward momentum into vertical movement to make him slide up the wall? This way the faster you run at a wall the higher up the wall you will slide (you may want to tweak gravity when sliding to make it less so the player slides more)
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    You need to track the hit, not have to press the button as soon as it hits. Give your player some time to hit it. ;)

    Code (csharp):
    1.  
    2. private var canWallJump : boolean=false;
    3. private var canWallJumpTime : float=0;
    4. private var canWallJumpDir : Vector3 = Vector3.zero;
    5. private var lastDirection : Vector3
    6.  
    7. function OnCollisionEnter(hit : Collision) {
    8.     if(controller.isGrounded == false){
    9.         for (var contact : ContactPoint in collision.contacts) {
    10.             if(hit.collider.gameObject.tag == "Wall"){
    11.                 canWallJump = true;
    12.                 canWallJumpTime = Time.time;
    13.                 canWallJumpDir = Vector3.Scale(Vector3(-1,0,-1), lastDirection);
    14.             }
    15.         }
    16.     }
    17. }
    18.  
    19. function Update(){
    20.     if(Time.time - canWallJumpTime > 0.25) canWallJump = false;
    21.     if(Input.GetButtonDown ("Jump")  canWallJump){
    22.         // calculate jump with a movement based off of canWallJumpDir
    23.     }
    24.    
    25.    
    26.     // calculate movement and apply it to the controller
    27.     lastDirection = movement;
    28. }
    29.  
    Edit: yeah, your going to have to have some way to capture the movement before it turns zero. So lastDirection should not be set if the magnitude of X and Z are zero...

    if(Vector2(movement.x, movement.z).magnitude > 0.001) lastDirection = movment;
     
    Last edited: Mar 16, 2012
  4. DatGameDesigner

    DatGameDesigner

    Joined:
    Mar 2, 2012
    Posts:
    6
    care you clairfy that a bit more?
     
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Every frame up to that point, you need to figure out at which rate of speed you are moving. If you actually are moving, then you need to capture that direction. (in this case, reverse the x and z so that it bounces off the wall the way it came)

    Now, when you hit a wall, you give the player some time to press the button. Because pressing it at the exact moment you hit it is very hard. (lets say about 0.25 seconds.)

    canWallJump is that we can wall jump
    canWallJumpTime is the time captured by the collider, so that we know to wait 0.25 seconds past it, if we go beyond that we can't jump
    canWallJumpDir holds the direction that we gathered from the last good direction that we moved.
     
  6. DatGameDesigner

    DatGameDesigner

    Joined:
    Mar 2, 2012
    Posts:
    6
    It doesn't seem recognize the collision unless I give apply gravity, but it starts to freak out when I do that.