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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Variable jump in C#

Discussion in 'Scripting' started by Cott, Sep 18, 2016.

  1. Cott

    Cott

    Joined:
    Nov 2, 2015
    Posts:
    5
    Hi, I have a code which is basically perfect for jump and doublejump but the character has a fixed jumping height. What I'm looking for is a "super mario jumping effect". Press the jump button really fast, jump a little, keep it pressed , jump a little higher and so on. (hope I explained myself well, if not, I'll try again).

    this is the code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CharController : MonoBehaviour
    5. {
    6.     [SerializeField] private float m_JumpForce;
    7.     [SerializeField] private float moveSpeed;
    8.     [SerializeField] private LayerMask m_WhatIsGround;
    9.    
    10.     private Transform m_GroundCheck;
    11.     private Transform m_GroundCheckS;
    12.     const float k_GroundedRadius = .15f;
    13.    
    14.     private bool cir_Grounded;
    15.     private bool cir_GroundedS;
    16.     private bool is_Grounded;
    17.     private bool doubleJump;
    18.    
    19.     private Rigidbody2D m_Rigidbody2D;
    20.    
    21.     // Use this for initialization
    22.     void Start()
    23.     {
    24.         m_GroundCheck = transform.Find("groundCheck");
    25.         m_GroundCheckS = transform.Find("groundCheckS");
    26.         m_Rigidbody2D = GetComponent<Rigidbody2D>();  
    27.  
    28.         gameObject.transform.position = new Vector2 (10, 30);
    29.     }
    30.    
    31.     void Update()
    32.     {
    33.         cir_Grounded = false;
    34.         cir_GroundedS = false;
    35.         is_Grounded = false;
    36.  
    37.         Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
    38.         for (int i = 0; i < colliders.Length; i++)
    39.         {
    40.             if (colliders[i].gameObject != gameObject)
    41.                 cir_Grounded = true;
    42.         }
    43.        
    44.         Collider2D[] colliders2 = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
    45.         for (int i = 0; i < colliders2.Length; i++)
    46.         {
    47.             if (colliders2[i].gameObject != gameObject)
    48.                 cir_GroundedS = true;
    49.         }
    50.        
    51.         if (cir_GroundedS && cir_Grounded)
    52.         {
    53.             is_Grounded = true;
    54.         }
    55.  
    56.         //m_Rigidbody2D.velocity = new Vector2 (moveSpeed, m_Rigidbody2D.velocity.y); //Moves the player horizontally
    57.  
    58.         if (Input.GetKeyDown ("space"))
    59.         {
    60.             if (is_Grounded)
    61.             {
    62.                 is_Grounded = false;
    63.                 cir_Grounded = false;
    64.                 cir_GroundedS = false;
    65.                 m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, 0);
    66.                 m_Rigidbody2D.AddForce (new Vector2(0f, m_JumpForce));
    67.                 doubleJump = true;
    68.             }
    69.             else if (doubleJump)
    70.             {
    71.                 m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, 0);
    72.                 m_Rigidbody2D.AddForce (new Vector2(0f, m_JumpForce));
    73.                 doubleJump = false;
    74.             }
    75.         }
    76.     }
    77. }
    Thanks for the attention
     
  2. Mike-Geig

    Mike-Geig

    Unity Technologies

    Joined:
    Aug 16, 2013
    Posts:
    226
    Try something like:

    Code (CSharp):
    1. float jumpForce = 3f;
    2.  
    3. //inside FixedUpdate
    4. if(Input.GetButton("Jump"))
    5. {
    6.     m_Rigidbody2D.AddForce(new Vector2(0f, jumpForce));
    7.     jumpForce -= .5f //or whatever amount
    8.     if(jumpForce < 0f)
    9.         jumpForce = 0f;
    10. }
    11.  
    12. //inside Update
    13. if(Input.GetButtonUp("Jump"))
    14. {
    15.     jumpForce = 3f; //go back to original power
    16. }
    This is just off the top of my head, but should get you going the right direction
     
    Last edited: Sep 19, 2016
    s16110 likes this.
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,198
    Put it inside FixedUpdate for results that won't vary wildly depending on framerate.
     
    Mike-Geig likes this.
  4. Mike-Geig

    Mike-Geig

    Unity Technologies

    Joined:
    Aug 16, 2013
    Posts:
    226
    Hah, good point! I wrote the code out quickly in notepad

    EDIT: The GetButton should be in fixed update, however the GetButtonUp should be in regular update since FixedUpdate can miss down and up checks
     
  5. Cott

    Cott

    Joined:
    Nov 2, 2015
    Posts:
    5
    Thank you for replying, as soon as I get them working I'll let you know. The solution concept is so easy, thanks again.
     
  6. Cott

    Cott

    Joined:
    Nov 2, 2015
    Posts:
    5
    I know, double post but need to ask you an opinion (actually if we can discuss about a code).
    Basically this it the Gamasutra page I took the idea of how to make a variable height jump the first time: http://www.gamasutra.com/blogs/DanielFineberg/20150825/244650/Designing_a_Jump_in_Unity.php

    this is the code in short (a simplified version of it):

    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4.     if(jumpButtonDown && !jumping)
    5.     {
    6.         jumping = true;
    7.         StartCoroutine(JumpRoutine());
    8.     }
    9. }
    10.  
    11. IEnumerator JumpRoutine()
    12. {
    13.     rigidbody.velocity = Vector2.zero;
    14.     float timer = 0;
    15.      
    16.     while(jumpButtonPressed && timer < jumpTime)
    17.     {
    18.         //Calculate how far through the jump we are as a percentage
    19.         //apply the full jump force on the first frame, then apply less force
    20.         //each consecutive frame
    21.          
    22.         float proportionCompleted = timer / jumpTime;
    23.         Vector2 thisFrameJumpVector = Vector2.Lerp(jumpVector, Vector2.zero, proportionCompleted);
    24.         rigidbody.AddForce(thisFrameJumpVector);
    25.         timer += Time.deltaTime;
    26.         yield return null;
    27.     }
    28.      
    29.     jumping = false;
    30. }
    31. }
    So, how in the world can they get the button pressed and put it in a while loop without make Unity crash (every time I tried it crashed). To compare the "while loop" with an "if statement" (that holds a "input.getkey"), the while loop never ends until the conditions are met, but what about the "if statement"? does it exits every frame but still holding the condition Input.getkey?
     
    Last edited: Sep 22, 2016
  7. TimHellmann

    TimHellmann

    Joined:
    Mar 6, 2011
    Posts:
    66
    Can somebody please tell me what this 'jumpVector' is and which values it should have? Plus, the way this C# code on gamasutra is written, it does not work for me at all. I change that first rigidbody.velocity to the new AddForce standard but the player never reacts to the pressed button. Only to the initial first frame push. Please help.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,198
    It's the direction you're jumping in!

    probably something along the lines of:

    Code (csharp):
    1. Vector2.up + currentVelocity.x;
    Note that I'm not normalizing the vector. That gives Mario rules, where a standing jump and a running jump gives the same height.
     
  9. Joboto

    Joboto

    Joined:
    Sep 12, 2013
    Posts:
    64
    I stumbled across this tutorial series recently while looking for an alternative solution to my problem.



    I remember it having a intuitive variable jump solution. I recommend a watch.

    Kind Regards,
    Joe