Player Only Grabs on to Right Walls...

Hi, I’m trying to get my player to wall jump. He grabs on to walls that are on his right just fine, but not walls that are on his left. Thanks for any help.
Also, this is my first project so feel free to spell it out for me.
I’m about to go out for tonight but will respond tomorrow morning!

My player movement code. (The wall jumping code is labelled, and at the start of the update function)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class PlayerMovement : MonoBehaviour
{
   public CharacterController2D controller;
   public Animator animator;

   public float runSpeed = 40f;

   float horizontalMove = 0f;
   bool jump = false;
   bool crouch = false;

   [SerializeField] private LayerMask m_WhatIsGround;                            // A mask determining what is ground to the character
    [SerializeField] private Transform m_GroundCheck;                            // A position marking where to check if the player is grounded.
const float k_GroundedRadius = .2f;
private bool m_Grounded;

public UnityEvent OnLandEvent;

   [SerializeField] private AudioSource jumpSoundEffect;
   [SerializeField] private AudioSource dashSoundEffect;

// //dash stuff
[SerializeField] Rigidbody2D rb;
   public float dashDistance = 10f;
   bool isDashing;
   float doubleTapTime;
   KeyCode lastKeyCode;
   float mx;

   [Header("Dashing")]
   [SerializeField] private float m_JumpForce = 400f;
   public bool canDash = true;
   public float dashingTime;
   public float dashSpeed;
   public float dashJumpIncrease;
   public float timeBtwDashes;

//double tap stuff
public bool singleTap = false;
     public bool doubleTap = false;
     bool tapping = false;
     float tapTime = 0;
     float duration = .4f;

     //walljump stuff
     public Transform wallGrabPoint;
     private bool canGrab, isGrabbing;
     private float gravityStore;

     void Start()
     {
         //wall jump stuff
         gravityStore = rb.gravityScale;
     }

    void Update()
    {

//handle wall jumping
canGrab = Physics2D.OverlapCircle(wallGrabPoint.position, .2f, m_WhatIsGround);

isGrabbing = false;
if(canGrab && !m_Grounded)
{
    if((transform.localScale.x == 1f && Input.GetAxisRaw("Horizontal") > 0))
    {
        isGrabbing = true;
    }
    if((transform.localScale.x == -1f && Input.GetAxisRaw("Horizontal") < 0))
    {
        isGrabbing = true;
    }
}

if(isGrabbing)
{
    rb.gravityScale = 0f;
    rb.velocity = Vector2.zero;
} else{
    rb.gravityScale = gravityStore;
}

        {
            if (Input.GetKeyDown(KeyCode.LeftShift))
            {
                DashAbility();
            }
        }

//double tap stuff
if (Input.GetKeyDown(KeyCode.LeftArrow))
         {
             if (tapping)
             {
                 doubleTap = true;
                 Debug.Log("DoubleTap");
                  DashAbility();
                 tapping = false;
             }
             else
             {
                 tapping = true;
                 tapTime = duration;
                
             }
         }
         if (tapping)
         {
             tapTime = tapTime - Time.deltaTime;
             if (tapTime <= 0)
             {
                 tapping = false;
                 singleTap = true;
                 Debug.Log("SingleTap");
             }
         }

         if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             if (tapping)
             {
                 doubleTap = true;
                 Debug.Log("DoubleTap");
                  DashAbility();
                 tapping = false;
             }
             else
             {
                 tapping = true;
                 tapTime = duration;
                
             }
         }
         if (tapping)
         {
             tapTime = tapTime - Time.deltaTime;
             if (tapTime <= 0)
             {
                 tapping = false;
                 singleTap = true;
                 Debug.Log("SingleTap");
             }
         }

       horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
//testing move for wall jump
//rb.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * runSpeed, rb.velocity.y);

animator.SetFloat("Speed", Mathf.Abs(horizontalMove));

       if (m_Grounded && (Input.GetButtonDown("Jump")))
       {
jump = true;
animator.SetBool("IsJumping", true);
jumpSoundEffect.Play();
       }

       if (Input.GetButtonDown("Crouch"))
       {
crouch = true;
       } else if (Input.GetButtonUp("Crouch"))
       {
           crouch = false;
       }
    }

public void OnLanding ()
{
    animator.SetBool("IsJumping", false);
}

public void OnCrouching (bool isCrouching)
{
    animator.SetBool("IsCrouching", isCrouching);
}

    void FixedUpdate()
    {

bool wasGrounded = m_Grounded;
        m_Grounded = false;

        // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
        // This can be done using layers instead but Sample Assets will not overwrite your project settings.
        Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
        for (int i = 0; i < colliders.Length; i++)
        {
            if (colliders[i].gameObject != gameObject)
            {
                m_Grounded = true;
                if (!wasGrounded)
                    OnLandEvent.Invoke();
            }
        }


        //Move our character
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
    }

public void StartWalking()
{
  runSpeed = 50f;
}
public void StopWalking()
{
runSpeed = 0f;
}

// IEnumerator Dash (float direction)
// {
//     isDashing = true;
//     rb.velocity = new Vector2(rb.velocity.x, 0f);
//     rb.AddForce(new Vector2(dashDistance * direction, 0f), ForceMode2D.Impulse);
//     float gravity = rb.gravityScale;
//     rb.gravityScale = 0f;
//     yield return new WaitForSeconds(0.4f);
//     isDashing = false;
//     rb.gravityScale = gravity;
// }

void DashAbility()
{
    if(canDash)
    {
        animator.SetBool("isDashing", true);
        StartCoroutine(Dash());
    }
}
IEnumerator Dash()
{
    dashSoundEffect.Play();
    canDash = false;
    runSpeed = dashSpeed;
    m_JumpForce = dashJumpIncrease;
    yield return new WaitForSeconds(dashingTime);
     animator.SetBool("isDashing", false);
    runSpeed = 50f;
    m_JumpForce = 800f;
    yield return new WaitForSeconds(timeBtwDashes);
    canDash = true;
}

void LateUpdate()
     {
         if (doubleTap) doubleTap = false;
         if (singleTap) singleTap = false;
     }

//      void Animation()
//      {
//          if (isDashing)
//          {
// animator.SetBool("isDashing", true);
//          }
//          else {
// animator.SetBool("isDashing", false);
//          }
//      }

}

Steps to success:

  • identify the chain of steps that cause the player to wall jump on the right

  • study how those values differ when grabbing on the left

  • engineer a solution

Yesterday I posted about testing floating point numbers for equality:

Don’t do that. Here’s why:

https://discussions.unity.com/t/876285/4

That might even be your entire problem, but you have to prove it first to know.

Debug.Log() will be your friend here.

Thanks for replying,

I now understand why “localScale == -1f” is bad, because it only works if the number is precisely -1, but I have no idea what I would replace it with
And I have studied the wall jumping on the right and it seems like it’s the exact same on the left, except flipped, that’s why I’m confused as to why it’s not working

Traditionally you keep track of all the state yourself.

private bool FacingLeft;

Then when you walk left / right, you ONLY change that bool and absolutely NOTHING else.

Later code elsewhere observes that bool and makes the agent face left or right by whatever means you are using: scaling, flipping, rotating, etc.

Thank you for taking the time to respond.
I do have a FacingRight bool in my CharacterController2D script, I’ll post that as well if I have an issue next time

Just a minute ago I actually got it to work by changing the
(transform.localScale.x == -1f) to
(transform.localScale.x == 1f)

I guess it has to be a positive 1 on both the left and the right. I have no idea how or why this works because I thought going left had to be negative. But I will take it and hopefully not run into problems later

You can see an object’s local scale in the inspector. Is it ever actually -1 or negative at all?

You should probably fix that lack of understanding, but of course it is up to you.

This is covered in Step #2 of my “how to use sample code properly” blurb:

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:

Tutorials are a GREAT idea. Tutorials should be used this way:

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!

Actually it’s always negative and never positive. Which I guess makes sense because he’s sticking to a wall opposite of the direction he’s going in (I guess???)

Thank you, I do practice some of these steps and I’m watching a plethora of tutorials and I try to actually understand what I’m watching. For the most part I do understand what line is making “this” do “that”, and for my first project, I have troubleshooted myself through dozens of issues

However, it is still like an alien language, and as thorough as I am, I’m working on a project for my grad program with deadlines so if I get something working, for NOW, I am keeping it moving. So I do ask dumb questions sometimes but I’m still learning quickly and eventually after enough dumb questions I’ll know what I’m doing

1 Like