Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

[PROBLEM] RigidBody velocity for Movement and Jump

Discussion in 'Scripting' started by rslnautic, May 20, 2017.

  1. rslnautic

    rslnautic

    Joined:
    Nov 16, 2015
    Posts:
    5
    Hi. I can move independently of the jump (The jump has two heights) and I can jump independently of the movement, but my problem is that when it comes to putting them together it does not work well neither the movement nor the jump.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System;
    6.  
    7. public class PlayerController : MonoBehaviour {
    8.  
    9.     #region StructsParams
    10.     [Serializable]
    11.     public struct MovementParams
    12.     {
    13.         public float speed; // Movement speed
    14.     }
    15.  
    16.     [Serializable]
    17.     public struct JumpParams
    18.     {
    19.         public float maxHeight;     // Maximum height of jump
    20.         public float minHeight;     // Maximum height of jump
    21.         public float heightTime;    // Maximum height of jump
    22.         public float gravityFactor; // Maximum height of jump
    23.     }
    24.     #endregion
    25.  
    26.     [Header("- Movement Settings -")]
    27.     public MovementParams movementParams;
    28.     [Header("- Jump Settings -")]
    29.     public JumpParams jumpParams;
    30.  
    31.     private float horizontal;
    32.     private float vertical;
    33.     private Vector3 movement;
    34.  
    35.     private float g1; // World gravity
    36.     private float g2; // Falling gravity
    37.     private float d;  // Distance traveled
    38.     private float a;  // Acceleration
    39.     private float v;  // Velocity
    40.     private float t;  // Time taken in s
    41.  
    42.     private float velocityJump;
    43.     private float minVelocityJump;
    44.     private Rigidbody _rigidbody;
    45.     public bool grounded;
    46.  
    47.  
    48.     // Use this for initialization
    49.     void Start () {
    50.         // I used the dynamics formulas to calculate the world gravity -> g1, falling gravity -> g2,
    51.         // the speed of jump according to the maximum height -> velocityJump and the speed of jump according to the minimum height -> minVelocityJump
    52.         d = jumpParams.maxHeight;
    53.         t = jumpParams.heightTime;
    54.         a =  d * t * t * 2; // d = ½ * a * t^2 -> a = 2 * d * t^2
    55.         g1 = a;
    56.         g2 = jumpParams.gravityFactor * a;
    57.         v = a * t;
    58.         velocityJump = v;
    59.      
    60.         d = jumpParams.minHeight;
    61.         t = jumpParams.heightTime;
    62.         a = d * t * t * 2; // d = ½ * a * t^2 -> a = 2 * d * t^2
    63.         g1 = a;
    64.         g2 = jumpParams.gravityFactor * a;
    65.         v = a * t;
    66.         minVelocityJump = v;
    67.  
    68.         _rigidbody = GetComponent<Rigidbody>();
    69.     }
    70.  
    71.     void FixedUpdate () {
    72.  
    73.         //Movement
    74.         horizontal = Input.GetAxis("Horizontal");
    75.         vertical = Input.GetAxis("Vertical");
    76.         movement = new Vector3(horizontal, 0f, vertical);
    77.         _rigidbody.velocity = movement * movementParams.speed;
    78.  
    79.         // Gravities
    80.         if(_rigidbody.velocity.y >= 0)
    81.         {
    82.             _rigidbody.velocity = Vector3.ProjectOnPlane(_rigidbody.velocity, Vector3.up) + _rigidbody.velocity.y * Vector3.up  - g1 * Vector3.up * Time.deltaTime;
    83.         } else {
    84.             _rigidbody.velocity = Vector3.ProjectOnPlane(_rigidbody.velocity, Vector3.up) + _rigidbody.velocity.y * Vector3.up - g2 * Vector3.up * Time.deltaTime;
    85.         }
    86.  
    87.         // If I'm grounded and press the key space -> Jump with velocityJump
    88.         if(Input.GetKeyDown(KeyCode.Space) && grounded)
    89.         {
    90.             _rigidbody.velocity = Vector3.ProjectOnPlane(_rigidbody.velocity, Vector3.up) + Vector3.up * velocityJump;
    91.         }
    92.  
    93.         // If I'm not grounded and release the key space and my velocity in "y" is greater than minVelocityJump -> Set the Jump with minVelocityJump
    94.         if (Input.GetKeyUp(KeyCode.Space) && !grounded && _rigidbody.velocity.y > minVelocityJump)
    95.         {
    96.             _rigidbody.velocity = Vector3.ProjectOnPlane(_rigidbody.velocity, Vector3.up) + Vector3.up * minVelocityJump;
    97.         }
    98.     }
    99.  
    100.     // Check if I'm grounded
    101.     private void OnCollisionStay(Collision collision)
    102.     {
    103.         grounded = true;
    104.     }
    105.  
    106.     private void OnCollisionExit(Collision collision)
    107.     {
    108.         grounded = false;
    109.     }
    110. }
    111.  
    Here you cand download the unity project;
    Thanks!
     

    Attached Files:

    Last edited: May 20, 2017