Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Simple way to have smooth jumping without rigid body! finally!

Discussion in 'General Discussion' started by Tomnnn, Jun 2, 2014.

  1. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    With much tinkering, I accidentally put together a very basic way to have your character controller (or other homebrew script) ascend gracefully instead of teleport and then descend. I know most of us figure out the slow falling gravity part, but the jumping is always a pain in the butt. So here's the solution I'd like to share with you all because a few hours of googling were useless to me and probably others.

    Code (CSharp):
    1. //keep camera on top of body
    2.         Vector3 camPos = body.position + (body.up*1);
    3.         transform.position = camPos;
    4.  
    5.         Vector3 movement = (transform.forward * moveZ * Time.deltaTime) + (transform.right * moveX * Time.deltaTime);
    6.         lastFlag = controller.collisionFlags;
    7.  
    8.         if(lastFlag == CollisionFlags.CollidedBelow)
    9.         {
    10.             movement.y = 0;
    11.         }
    12.         else if(lastFlag == CollisionFlags.None)
    13.         {
    14.             movement.y -= (gravity * Time.deltaTime) - (vertical_force * Time.deltaTime);
    15.             movement.y = Mathf.Clamp(movement.y, maxFall*-1, maxFall*10);
    16.             if(vertical_force > 0)
    17.             {
    18.                 vertical_force -= gravity * Time.deltaTime;
    19.             }
    20.             if(vertical_force < 0)
    21.             {
    22.                 vertical_force = 0;
    23.             }
    24.         }
    25.  
    26.         if(jump)
    27.         {
    28.             vertical_force = 20;
    29.         }
    30.  
    31.         //transform.position += movement;
    32.         controller.Move(movement);
    No coroutines :D just a slight modification to the gravity line we all come up with. My gravity force is 10, so I chose a vertical/jump force of 20. If you choose 10 or less, you will not jump, because gravity will immediately overcome your vertical force. Jump is set in another function that polls the keyboard for input.

    Code (CSharp):
    1. jump = (lastFlag == CollisionFlags.CollidedBelow) && Input.GetKey(KeyCode.Space);
    I don't know what the norm is for using Time.deltaTime in multiple places instead of making a temporary variable at the beginning of each update, but you get the idea here.
     
    PromiseDEV, Netuddmeg and Brendonm17 like this.
  2. mabasumoh_unity

    mabasumoh_unity

    Joined:
    Jul 17, 2019
    Posts:
    1
    Know this is kinda late, but where is the code located? is it in void update, or is it just code to plug in?
     
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,868
    5 years is "kinda late"? Also this guy hasn't been seen on the forum since 2016.
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,307
    You could put it in update, but The poster assumes that you have some variables (such as "controller") already declared and assigned a value. Don't expect to copy and paste it and it necessarily work as-is.
     
  5. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,716
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,071
    For a short moment I was hopefully he had reappeared... then disappointment when I realized it was a necro.