Search Unity

Is it possible to script a stylised jump arc

Discussion in 'Scripting' started by shmomo, Sep 13, 2012.

  1. shmomo

    shmomo

    Joined:
    Oct 11, 2011
    Posts:
    127
    To jump, im using the character controller, adding an amount to my y position then using time.deltaTime to subtract gravity to bring me back grounded. This creates an arc for the jump.

    Is it possible to script a more stylised arc for the jump?

    One the moves up fast, hovers in the air for a bit then moves fast back down to the ground?

    Many thanks,

    J
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    sure, but it's easier if you avoid calculating with the gravity during the jump.

    Just create a curve for your jump, how ever you want. Go through the jump arc until completion, and then turn gravity back on when it's done.

    Some things you'll want to consider...

    Stop jump if you land on ground (jumping up onto a ledge or something would cause this)

    You can let gravity take care of the drop part of the arc... For instance you could have a jump arc that brings you in the air and floats for a bit... then stop, letting gravity bring you back to the ground when it's done


    something like this (unityscript)... it's slapped together in notepad, so no guarantees

    Code (csharp):
    1.  
    2. private var _inJump:boolean;
    3. private var _jumpTime:float;
    4.  
    5. var jumpHeight:float = 2;
    6. var jumpRaiseDur:float = 0.3;
    7. var jumpFloatDur:float = 0.7;
    8.  
    9. function Update()
    10. {
    11.     if(!_inJump  Input.GetButtonDown("Jump"))
    12.     {
    13.         _inJump = true;
    14.         _jumpTime = 0;
    15.     }
    16.  
    17.     if(_inJump  _jumpTime < jumpRaiseDur + jumpFloatDur)
    18.     {
    19.         var mv = Vector3.up;
    20.         mv *= jumpHeight / jumpRaiseDur;//scale so we raise the total over raiseDur...
    21.  
    22.         //we only rise for jumpRaiseDur seconds
    23.         //this means we can spend jumpFloatDur seconds floating in the air
    24.         if(_jumpTime > jumpRaiseDur) mv.y = 0;
    25.         mv *= Time.deltaTime;
    26.         controller.Move(mv);
    27.  
    28.         _jumpTime += Time.deltaTime;
    29.         if(_jumpTime > jumpRaiseDur + jumpFloatDur) _inJump = false;
    30.     }
    31.     else
    32.     {
    33.         //do regular move stuff...
    34.         controller.Move(someDir + Physics.gravity * Time.deltaTime);
    35.     }
    36. }
    37.  
     
    Last edited: Sep 13, 2012
  3. shmomo

    shmomo

    Joined:
    Oct 11, 2011
    Posts:
    127
    great thanks!

    It works well in a linear way.

    when you say create the curve for your jump, do you mean script it or animate it?

    If it is scripted do you know of a resource that might help for scripting arcs?

    Thanks for the awesome response

    J