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 Problems with 2D Wall-Jumping

Discussion in '2D' started by themochawave, Aug 19, 2023.

  1. themochawave

    themochawave

    Joined:
    Sep 15, 2022
    Posts:
    5
    I've been trying to create a fluid 2D platformer with an AI (which, yes, is a very big mistake), and I cannot seem to create smooth wall-jumping even if I make it myself. Can anyone find any problems with this? (besides the fact that i used an AI...)

    Here is my entire Player Movement script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public float moveSpeed = 5f;
    8.     public float jumpForce = 10f;
    9.     public float wallJumpForce = 10f;
    10.     public float wallJumpMoveStopDuration = 0.2f;
    11.     public float airControlFactor = 0.7f;
    12.  
    13.     public float raycastLength = 0.1f;
    14.     public int maxJumps = 1;
    15.  
    16.     public LayerMask groundLayer;
    17.     public LayerMask wallLayer;
    18.  
    19.     private Rigidbody2D rb;
    20.     private bool isFacingRight = true;
    21.     private bool isGrounded = false;
    22.     private bool isTouchingWall = false;
    23.     private bool isWallSliding = false;
    24.     private int jumpsRemaining = 0;
    25.     private float wallJumpStopTime;
    26.  
    27.     private void Start()
    28.     {
    29.         rb = GetComponent<Rigidbody2D>();
    30.         jumpsRemaining = maxJumps;
    31.     }
    32.  
    33.     private void Update()
    34.     {
    35.         isGrounded = IsGrounded();
    36.         isTouchingWall = IsTouchingWall();
    37.  
    38.         float horizontalInput = Input.GetAxis("Horizontal");
    39.  
    40.         if (isGrounded)
    41.         {
    42.             jumpsRemaining = maxJumps;
    43.             isWallSliding = false;
    44.         }
    45.  
    46.         if (isTouchingWall && !isGrounded && rb.velocity.y <= 0)
    47.         {
    48.             isWallSliding = true;
    49.             jumpsRemaining = maxJumps;
    50.         }
    51.         else
    52.         {
    53.             isWallSliding = false;
    54.         }
    55.  
    56.         if (Input.GetButtonDown("Jump"))
    57.         {
    58.             if (isGrounded)
    59.             {
    60.                 jumpsRemaining = maxJumps - 1;
    61.                 rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    62.             }
    63.             else if (isWallSliding && jumpsRemaining > 0)
    64.             {
    65.                 WallJump();
    66.                 jumpsRemaining--;
    67.             }
    68.             else if (jumpsRemaining > 0)
    69.             {
    70.                 jumpsRemaining--;
    71.                 rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    72.             }
    73.         }
    74.  
    75.         float targetVelocityX = horizontalInput * moveSpeed;
    76.  
    77.         if (isWallSliding)
    78.         {
    79.             rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -moveSpeed, float.MaxValue));
    80.         }
    81.         else
    82.         {
    83.             if (!isGrounded && !isWallSliding)
    84.             {
    85.                 targetVelocityX *= airControlFactor;
    86.             }
    87.  
    88.             rb.velocity = new Vector2(targetVelocityX, rb.velocity.y);
    89.         }
    90.  
    91.         if ((targetVelocityX > 0 && !isFacingRight) || (targetVelocityX < 0 && isFacingRight))
    92.         {
    93.             Flip();
    94.         }
    95.     }
    96.  
    97.     private void WallJump()
    98.     {
    99.         isWallSliding = false;
    100.         rb.velocity = new Vector2((isFacingRight ? 1 : -1) * wallJumpForce, jumpForce);
    101.         Flip();
    102.         wallJumpStopTime = Time.time + wallJumpMoveStopDuration;
    103.     }
    104.  
    105.     private bool IsGrounded()
    106.     {
    107.         RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, raycastLength, groundLayer);
    108.         return hit.collider != null;
    109.     }
    110.  
    111.     private bool IsTouchingWall()
    112.     {
    113.         RaycastHit2D hit = Physics2D.Raycast(transform.position, isFacingRight ? Vector2.right : Vector2.left, raycastLength, wallLayer);
    114.         return hit.collider != null;
    115.     }
    116.  
    117.     private void Flip()
    118.     {
    119.         isFacingRight = !isFacingRight;
    120.         Vector3 scale = transform.localScale;
    121.         scale.x *= -1;
    122.         transform.localScale = scale;
    123.     }
    124. }
    125.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,560
    AIs are NOT software engineers. They are Large Language Models.

    If you see someone saying "Coding is AI's super power!" then what they are doing is saying "Look, I found some socks, and I can use them as gloves!"

    Now everybody is standing around with socks on their hands saying "These socks are just like gloves!"

    I'm gonna say "No, you have socks on your hands, those aren't gloves. Try picking your nose. See? It doesn't work unless you have really big nostrils."

    Let me summarize how this post appears to me.

    - You're too lazy to work through a tutorial so you delegated it to AI.
    - Now you're too lazy to fix what the AI has given you so you have delegated it to us.

    I'm gonna go out on a limb here and say that nobody here is likely to be too interested in untangling AI code for you.

    Instead, why don't you be like every other programmer through history and work through THREE (3) separate tutorials on wall jumping and see how it goes from there?

    The principle isn't hard, you seem to have enough time to play with AI and post to the forum, why not redirect your efforts to a proven known solution to learning?

    Imphenzia: How Did I Learn To Make Games:



    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  3. themochawave

    themochawave

    Joined:
    Sep 15, 2022
    Posts:
    5
    you know, this could've been shortened by a lot. i'm also not having any compiling errors, thank you very much. i also know how to identify errors as well.

    make sure you don't assume certain things, because like i said in the post, i have tried to fix the AI's code myself.

    please read the whole message before you start writing paragraphs. thank you!

    by the way i started the game with AI because i wanted to see if it's better at coding than me. that's where your assumptions come in. because again, i've tried to fix it.
     
    Last edited: Aug 19, 2023
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,560
    Fixing anything generally consists of:

    - defining what is broken

    - finding the parts involved

    - finding what is not correct about them

    - fixing that

    You haven't even done part #1 beyond saying,

    What does that mean? It doesn't jump at all? It jumps but stutters? It embeds itself in the wall like a wet wadd of toilet paper instead of jumping?

    Again, standard troubleshooting:

    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 (smooth wall jumper)
    - what you tried (above)
    - what you expected to happen (smooth wall jumping)
    - what actually happened, log output, variable values, velocities, distances, times... and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)


    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?
     
    stalyan77 likes this.