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. Dismiss Notice

Question Doublejump after coyotetime and wallsliding ??

Discussion in '2D' started by traemand3000, Aug 9, 2023.

  1. traemand3000

    traemand3000

    Joined:
    Jul 29, 2023
    Posts:
    1
    Im having trouble implementing the ability to doublejump after coyotetime and while wallsliding/walljumping. I'm very much a n00b when it comes to coding.

    here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class NewPlayerMovement : MonoBehaviour
    4.  
    5. {
    6.     private float horizontal;
    7.     private float speed = 12f;
    8.     private float jumpingPower = 20f;
    9.     private bool isFacingRight = true;
    10.  
    11.     private bool isWallSliding;
    12.     private float wallSlidingSpeed = 2f;
    13.  
    14.     private float coyoteTime = 0.2f;
    15.     private float coyoteTimeCounter;
    16.  
    17.     private bool doubleJump;
    18.  
    19.     private bool isWallJumping;
    20.     private float wallJumpingDirection;
    21.     private float wallJumpingTime = 0.2f;
    22.     private float wallJumpingCounter;
    23.     private float wallJumpingDuration = 0.4f;
    24.     private Vector2 wallJumpingPower = new Vector2(8f, 16f);
    25.  
    26.     [SerializeField] private Rigidbody2D rb;
    27.     [SerializeField] private Transform groundCheck;
    28.     [SerializeField] private LayerMask groundLayer;
    29.     [SerializeField] private Transform wallCheck;
    30.     [SerializeField] private LayerMask wallLayer;
    31.  
    32.     private void Update()
    33.     {
    34.         horizontal = Input.GetAxisRaw("Horizontal");
    35.  
    36.         //Coyotetime
    37.  
    38.         if (IsGrounded())
    39.         {
    40.             coyoteTimeCounter = coyoteTime;
    41.         }
    42.         else
    43.         {
    44.             coyoteTimeCounter -= Time.deltaTime;
    45.         }
    46.  
    47.         // Doublejump
    48.  
    49.         if (Input.GetButtonDown("Jump") && IsGrounded())
    50.         {
    51.             doubleJump = false;
    52.         }
    53.  
    54.             if (Input.GetButtonDown("Jump"))
    55.         {
    56.             if (IsGrounded() || doubleJump)
    57.             {
    58.                 rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    59.  
    60.                 doubleJump = !doubleJump;
    61.             }
    62.         }
    63.  
    64.         if (coyoteTimeCounter > 0f && Input.GetButtonDown("Jump"))
    65.         {
    66.             rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    67.         }
    68.  
    69.         if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
    70.         {
    71.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    72.  
    73.             coyoteTimeCounter = 0f;
    74.         }
    75.  
    76.         WallSlide();
    77.         WallJump();
    78.  
    79.         if (!isWallJumping)
    80.         {
    81.             Flip();
    82.         }
    83.     }
    84.  
    85.     private void FixedUpdate()
    86.     {
    87.         if (!isWallJumping)
    88.         {
    89.             rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    90.         }
    91.     }
    92.  
    93.     private bool IsGrounded()
    94.     {
    95.         return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    96.     }
    97.  
    98.     private bool IsWalled()
    99.     {
    100.         return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer);
    101.     }
    102.  
    103.     private void WallSlide()
    104.     {
    105.         if (IsWalled() && !IsGrounded() && horizontal != 0f)
    106.         {
    107.             isWallSliding = true;
    108.             rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
    109.         }
    110.         else
    111.         {
    112.             isWallSliding = false;
    113.         }
    114.     }
    115.  
    116.     private void WallJump()
    117.     {
    118.         if (isWallSliding)
    119.         {
    120.             isWallJumping = false;
    121.             wallJumpingDirection = -transform.localScale.x;
    122.             wallJumpingCounter = wallJumpingTime;
    123.  
    124.             CancelInvoke(nameof(StopWallJumping));
    125.         }
    126.         else
    127.         {
    128.             wallJumpingCounter -= Time.deltaTime;
    129.         }
    130.  
    131.         if (Input.GetButtonDown("Jump") && wallJumpingCounter > 0f)
    132.         {
    133.             isWallJumping = true;
    134.             rb.velocity = new Vector2(wallJumpingDirection * wallJumpingPower.x, wallJumpingPower.y);
    135.             wallJumpingCounter = 0f;
    136.  
    137.             if (transform.localScale.x != wallJumpingDirection)
    138.             {
    139.                 isFacingRight = !isFacingRight;
    140.                 Vector3 localScale = transform.localScale;
    141.                 localScale.x *= -1f;
    142.                 transform.localScale = localScale;
    143.             }
    144.  
    145.             Invoke(nameof(StopWallJumping), wallJumpingDuration);
    146.         }
    147.     }
    148.  
    149.     private void StopWallJumping()
    150.     {
    151.         isWallJumping = false;
    152.     }
    153.  
    154.     private void Flip()
    155.     {
    156.         if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
    157.         {
    158.             isFacingRight = !isFacingRight;
    159.             Vector3 localScale = transform.localScale;
    160.             localScale.x *= -1f;
    161.             transform.localScale = localScale;
    162.         }
    163.     }
    164. }
     
    Last edited: Aug 9, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I'm not sure how you want walljumping to integrate here, but I suppose it would just be any touching to reset the jump ability??

    Here was my Coyote time / Double (or any number of) jump reference. Full source linked in video comments:



    Otherwise...

    isn't really enough of a description.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.