Search Unity

Player controls script doesn't transition from jump to 2nd jump properly

Discussion in 'Scripting' started by denissuu, Mar 19, 2020.

  1. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162
    Hi there everyone! I have a pretty simple setup over here where I'm trying to figure out why my jump count doesn't work properly in my player controls.On the first press it was supposed to Jump but the value of it stays at zero pretty much unless I spam click the jump a bit more.

    I want to make it so as soon as you hit the first jump the value stays at one until that animation finishes and he starts touching the ground again, not keep staying at zero from right when it just started and till the end.The value in between the grounded states should be 1, not still zero.It seems that the groundcheck resets the value back to zero even before starting to touch the ground again.How may I go over this issue in any sort of way so this doesn't keep on happening?

    Here is the entire script for an in depth look based on what's really happening.

    Some reference for the code :

    condition 0 is idle
    condition 1 is running
    condition 2 is jumping
    and condition 3 is the 2nd jump

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Zero_Controls : MonoBehaviour
    6. {
    7.     public float runSpeed = 10f;
    8.     public Rigidbody2D player;
    9.     Animator anim;
    10.     BoxCollider2D boxcoll;
    11.     [SerializeField] LayerMask platformLayerMask;
    12.     int jumpCount;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         boxcoll = GetComponent<BoxCollider2D>();
    17.         anim = GetComponent<Animator>();
    18.         player = GetComponent<Rigidbody2D>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (Input.GetMouseButton(0) && isGrounded())
    25.         {
    26.             anim.SetInteger("condition", 1);
    27.             player.velocity = new Vector2(runSpeed, player.velocity.y);
    28.         }
    29.         else if(isGrounded())
    30.         {
    31.             jumpCount = 0;
    32.             anim.SetInteger("condition", 0);
    33.             player.velocity = new Vector2(0f, player.velocity.y);
    34.         }
    35.         if(Input.GetMouseButtonDown(1) && isGrounded())
    36.         {
    37.             anim.SetInteger("condition", 2);
    38.         }
    39.         if (Input.GetMouseButtonDown(1) && jumpCount >= 1)
    40.         {
    41.             anim.SetInteger("condition", 3);
    42.         }
    43.         if (Input.GetMouseButtonDown(1))
    44.         {
    45.             jumpCount++;
    46.         }
    47.  
    48.         bool isGrounded()
    49.         {
    50.             float num = 0.5f;
    51.             RaycastHit2D raycastHit2D = Physics2D.BoxCast(boxcoll.bounds.center, boxcoll.bounds.size, 0f, Vector2.down, num, platformLayerMask);
    52.             Color color = (!(raycastHit2D.collider != null)) ? Color.red : Color.green;
    53.             UnityEngine.Debug.DrawRay(boxcoll.bounds.center + new Vector3(boxcoll.bounds.extents.x, 0f), Vector2.down * (boxcoll.bounds.extents.y + num), color);
    54.             UnityEngine.Debug.DrawRay(boxcoll.bounds.center - new Vector3(boxcoll.bounds.extents.x, 0f), Vector2.down * (boxcoll.bounds.extents.y + num), color);
    55.             UnityEngine.Debug.DrawRay(boxcoll.bounds.center - new Vector3(boxcoll.bounds.extents.x, boxcoll.bounds.extents.y + num), Vector2.right * boxcoll.bounds.extents.x, color);
    56.             return raycastHit2D.collider != null;
    57.         }
    58.     }
    59. }
    60.  

    Thanks for the help guys!
    You rock! :)
     
  2. maheshputra

    maheshputra

    Joined:
    Aug 10, 2018
    Posts:
    14
    Maybe, it's because when you start jumping, the isGrounded() function still return true and execute in :

    Then, you could try to add more codes for when you start jumping for the first time, there's a short period of time when the isGrounded() function will return false. Maybe until the boxcast fully not touching the platformlayer

    Sorry for my bad english
     
  3. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162

    I mean, I understand that, but how may I overwrite it's value?Will it work by using a Coroutine?Or does it need to be integrated in the function itself?
     
  4. maheshputra

    maheshputra

    Joined:
    Aug 10, 2018
    Posts:
    14
    Could try something like this

    Code (CSharp):
    1. [SerializeField] private float disableTime;
    2.     private bool disabled;
    3.     private Animator anim;
    4.     private BoxCollider2D boxcoll;
    5.  
    6.     private void Update()
    7.     {
    8.         if (Input.GetMouseButtonDown(1) && isGrounded())
    9.         {          
    10.             StartCoroutine(disableGroundCheck());
    11.             anim.SetInteger("condition", 2);
    12.         }
    13.     }
    14.  
    15.     IEnumerator disableGroundCheck() {
    16.         disabled = true;
    17.         yield return new WaitForSeconds(disableTime);
    18.         disabled = false;
    19.     }
    20.  
    21.     bool isGrounded()
    22.     {
    23.         if (disabled)
    24.             return false;
    25.  
    26.         float num = 0.5f;
    27.         RaycastHit2D raycastHit2D = Physics2D.BoxCast(boxcoll.bounds.center, boxcoll.bounds.size, 0f, Vector2.down, num, platformLayerMask);
    28.         Color color = (!(raycastHit2D.collider != null)) ? Color.red : Color.green;
    29.         UnityEngine.Debug.DrawRay(boxcoll.bounds.center + new Vector3(boxcoll.bounds.extents.x, 0f), Vector2.down * (boxcoll.bounds.extents.y + num), color);
    30.         UnityEngine.Debug.DrawRay(boxcoll.bounds.center - new Vector3(boxcoll.bounds.extents.x, 0f), Vector2.down * (boxcoll.bounds.extents.y + num), color);
    31.         UnityEngine.Debug.DrawRay(boxcoll.bounds.center - new Vector3(boxcoll.bounds.extents.x, boxcoll.bounds.extents.y + num), Vector2.right * boxcoll.bounds.extents.x, color);
    32.         return raycastHit2D.collider != null;
    33.     }
    and manually set disabled time
     
    denissuu likes this.
  5. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162


    That actually worked.Thank you so much for the help! I really appreciate it!
     
    maheshputra likes this.