Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Movement whilst jumping (Help)

Discussion in 'Scripting' started by BigBadSlug, Oct 15, 2019.

  1. BigBadSlug

    BigBadSlug

    Joined:
    Oct 15, 2019
    Posts:
    4
    Hi all,

    I've recently begun trying to learn how to make an FPSController from scratch and for the most part its going well.

    However, I've run into an issue with movement whilst jumping. I would like to allow the player to move slightly whilst mid-air, I already have a rough idea on how to implement this but as far as changing what I've got I'm stuck. I've tried rearranging my code so the jump check is performed, then movement is applied but I can't seem to get it to work. I've also tried moving the jump event into player movement without success. any suggestions or advice are welcome.

    EDIT: Upon looking at this further, I'm thinking I've perhaps done this in a bit too much of a restrictive manner.

    Here's what I've got so far. Ignore the comments, they're just mental notes for future reference :p

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerMove : MonoBehaviour
    5. {
    6.      //Left and Right keys
    7.      [Header("Movement Settings")]
    8.      [SerializeField] private string horizontalInputName;
    9.      //Forward and back keys/W-S keys
    10.      [SerializeField] private string verticalInputName;
    11.      private float movementSpeed;
    12.      [SerializeField] private float walkSpeed;
    13.      [SerializeField] private float runSpeed;
    14.      [SerializeField] private float runBuildUpSpeed;
    15.      [SerializeField] private KeyCode runKey;
    16.      [Header("Slope Settings")]
    17.      [Tooltip("Effects how much force is applied on a slope. The higher, the steeper a player can climb.")]
    18.      [SerializeField] private float slopeForce;
    19.      [Tooltip("Effects how smoothly the player can climb")]
    20.      [SerializeField] private float slopeForceRayLength;
    21.      private CharacterController characterController;
    22.      [Header("Jump Settings")]
    23.      //Graph input that allows customisation of how the jump behaves.
    24.      [SerializeField] private AnimationCurve jumpFallOff;
    25.      [SerializeField] private float jumpMultiplier;
    26.      [SerializeField] private KeyCode jumpKey;
    27.      private bool isJumping;
    28.      private void Awake()
    29.      {
    30.          //Script is attached to player body, able to access directly.
    31.          characterController = GetComponent<CharacterController>();
    32.      }
    33.      private void Update()
    34.      {
    35.          PlayerMovement();
    36.          JumpInput();
    37.      }
    38.      private void PlayerMovement()
    39.      {
    40.          //Getting axis for both inputs, movement speed is taken into account below in the SimpleMove function.
    41.          float vertInput = Input.GetAxis(verticalInputName);
    42.          float horizInput = Input.GetAxis(horizontalInputName);
    43.          //Vertical is forward due to + being forward, - being back. (See scene view and select player while playing for demo)
    44.          Vector3 forwardMovement = transform.forward * vertInput;
    45.          //Horizontal is right due to + being right, - being left. (See scene view and select player while playing for demo)
    46.          Vector3 rightMovement = transform.right * horizInput;
    47.          //SimpleMove applies time.deltaTime automatically, we use ClampMagnitude to keep diagonal movement the same speed as usual movement.
    48.          characterController.SimpleMove(Vector3.ClampMagnitude(forwardMovement + rightMovement, 1.0f) * movementSpeed);
    49.      
    50.          if(( vertInput != 0 || horizInput != 0 ) && OnSlope()) { characterController.Move(Vector3.down * characterController.height / 2 * slopeForce * Time.deltaTime); }
    51.          SetMovementSpeed();
    52.      }
    53.      private void SetMovementSpeed()
    54.      {
    55.          if (Input.GetKey(runKey))
    56.          {
    57.              movementSpeed = Mathf.Lerp(movementSpeed, runSpeed, Time.deltaTime * runBuildUpSpeed);
    58.          }
    59.          else
    60.          {
    61.              movementSpeed = Mathf.Lerp(movementSpeed, walkSpeed, Time.deltaTime * runBuildUpSpeed);
    62.          }
    63.      }
    64.      private bool OnSlope()
    65.      {
    66.          if (isJumping) { return false; }
    67.          RaycastHit hit;
    68.          //Fires raycast below player to check if they are on a slope.
    69.          if(Physics.Raycast(transform.position, Vector3.down, out hit, characterController.height / 2 * slopeForceRayLength))
    70.          {
    71.              if(hit.normal != Vector3.up) { return true; }
    72.          }
    73.          return false;
    74.      }
    75.      private void JumpInput()
    76.      {
    77.          if (Input.GetKey(jumpKey) && !isJumping)
    78.          {
    79.              isJumping = true;
    80.              StartCoroutine(JumpEvent());
    81.          }
    82.      }
    83.      private IEnumerator JumpEvent()
    84.      {
    85.          //Setting to sloplimit = 90.0f fixes an issue with glitching when jumping against walls.
    86.          characterController.slopeLimit = 90.0f;
    87.          float timeInAir = 0.0f;
    88.          do
    89.          {
    90.              float jumpForce = jumpFallOff.Evaluate(timeInAir);
    91.              characterController.Move(Vector3.up * jumpForce * jumpMultiplier * Time.deltaTime);
    92.              //Adds a single second to timeInAir
    93.              timeInAir += Time.deltaTime;
    94.              yield return null;
    95.          } while (!characterController.isGrounded && characterController.collisionFlags != CollisionFlags.Above);
    96.          characterController.slopeLimit = 45.0f;
    97.          isJumping = false;
    98.      }
    99. }
     
    Last edited: Oct 15, 2019