Search Unity

[ANSWERED] How do you force the player to keep moving forward when they jump?

Discussion in 'Scripting' started by michaelcapone, Jul 31, 2015.

  1. michaelcapone

    michaelcapone

    Joined:
    Aug 16, 2012
    Posts:
    12
    I am using a basic jump script to make my character jump. I am checking to make sure he is grounded before jumping to prevent the multiple jump scenario. I am also preventing the changing of rotation while they jump (to prevent them from 'turning' during a jump).

    Everything works great, but one thing bugs me. If the player is running (by pressing the right arrow key in my game) and then jumps he jumps forward, but if he let's go of the right arrow key he stops in mid-air. He can also jump straight up and then press the right arrow key to move forward .

    How can I make it so that if the person is already moving forward when they jump, they are forced to keep moving forward until they are back on solid ground?

    Here is my code in C-Sharp. Thanks for taking a look:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerJump : MonoBehaviour {
    5.  
    6.     public float distToGround;
    7.     public float jumpSpeed;
    8.  
    9.     private Rigidbody rb;
    10.     private CapsuleCollider collider;
    11.  
    12.     void Awake()
    13.     {
    14.         jumpSpeed = 6.0f;
    15.  
    16.         rb = GetComponent<Rigidbody>();
    17.         collider = GetComponent<CapsuleCollider>();
    18.     }
    19.  
    20.     void Start()
    21.     {
    22.         distToGround = collider.bounds.extents.y;  // get the distance to ground
    23.     }
    24.  
    25.     void Update ()
    26.     {
    27.         if (Input.GetKeyDown (KeyCode.Space) && IsGrounded())
    28.         {
    29.             rb.velocity += Vector3.up * jumpSpeed;
    30.         }
    31.     }
    32.  
    33.     bool IsGrounded()
    34.     {
    35.         return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
    36.     }
    37. }
     
    mosulimo likes this.
  2. Fantastic Worlds Studio

    Fantastic Worlds Studio

    Joined:
    Apr 26, 2015
    Posts:
    48
    The problem isn't in your jumping code, but probably in your code for moving right or left.
     
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    By adding something like "isGrounded", you're essentially creating a state machine where movement differs depending on the object's current state. That being the case, it's easy enough to make the movement differ in that, while not in the "isGrounded" state, player input no longer affects it. This is a common enough setup.

    Since you're going to be referencing your state machine from both this script and your "general movement" script, there's really no need to separate them. Give jump its own function rather than being in update and move all input-related commands to a higher-level script called PlayerInput or something which just calls functions in your "player" object's CharacterMovement script (or w/e it's called), which is the one you'll combine this one with.
     
  4. michaelcapone

    michaelcapone

    Joined:
    Aug 16, 2012
    Posts:
    12
    Thank you. Taking your advice Lysander I was able to make my player continue to move forward while jumping even without the user pressing a movement key.