Search Unity

Keeping momentum during jump?

Discussion in 'Physics' started by Enoai, Apr 24, 2017.

  1. Enoai

    Enoai

    Joined:
    Apr 24, 2017
    Posts:
    3
    Hello, I am here to seek how I would go about achieving this goal, I have researched around looking for how this is done and seen rigid body's but I've never used them, so if possible can someone explain how I cna adapt my following code to do this.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerController : MonoBehaviour {
    7.  
    8.     public float defaultmoveSpeed = 8.00f;
    9.     public float moveSpeed = 8.00f;
    10.     public float jumpSpeed = 15.00f;
    11.     public float gravity = 20.0f;
    12.     public float sprintSpeed = 16.00f;
    13.  
    14.  
    15.  
    16.     private Vector3 moveDirection = Vector3.zero;
    17.  
    18.     private CharacterController playerController;
    19.  
    20.  
    21.     // Use this for initialization
    22.     void Start () {
    23.         playerController = GetComponent<CharacterController>();
    24.  
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void Update () {
    29.         //Movement Section
    30.         playerMovement();
    31.  
    32.  
    33.         //methods to which should only be used when player is grounded
    34.         if (playerController.isGrounded)
    35.         {
    36.             jumpMethod();
    37.         }
    38.        
    39.     }
    40.  
    41.  
    42.  
    43.  
    44.  
    45.  
    46.     public void jumpMethod()
    47.     {
    48.         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    49.         moveDirection = transform.TransformDirection(moveDirection);
    50.         moveDirection *= moveSpeed;
    51.  
    52.         if (Input.GetButtonDown("Jump"))
    53.             moveDirection.y = jumpSpeed;
    54.     }
    55.  
    56.  
    57.     public void playerMovement()
    58.     {
    59.         moveDirection.y -= gravity * Time.deltaTime;
    60.         playerController.Move(moveDirection * Time.deltaTime);
    61.  
    62.         if (Input.GetKey("w"))
    63.         {
    64.             transform.Translate((Vector3.forward) * moveSpeed * Time.deltaTime);
    65.         }
    66.  
    67.         if (Input.GetKey("S"))
    68.         {
    69.             transform.Translate((Vector3.back) * moveSpeed * Time.deltaTime);
    70.         }
    71.  
    72.         if (Input.GetKey("A"))
    73.         {
    74.             transform.Translate((Vector3.left) * moveSpeed * Time.deltaTime);
    75.         }
    76.  
    77.         if (Input.GetKey("D"))
    78.         {
    79.             transform.Translate((Vector3.right) * moveSpeed * Time.deltaTime);
    80.         }
    81.  
    82.         // Sprint key function
    83.         if (Input.GetKey("left shift") && Input.GetKey("w"))
    84.         {
    85.             moveSpeed = sprintSpeed;
    86.  
    87.         }
    88.         else
    89.         {
    90.  
    91.             moveSpeed = defaultmoveSpeed;
    92.  
    93.         }
    94.     }
    95. }
    96.  
    97.  
     
    Last edited: Apr 24, 2017
  2. Enoai

    Enoai

    Joined:
    Apr 24, 2017
    Posts:
    3
    The Goal I want to achieve just so everyone knows is the ability for the user to keep momentum when in air, so if they sprint they'll still be moving that direction when i nthe air.