Wall Jump Code help

Hey guys.

So i’m setting this code for this 2D platformer where you are basically locked inside 4 walls each level.

I have it set up so my player can jump once when he touches a wall and even falls down after some time of being wall sliding but what i need is a way to tell me code that the player can’t jump using the x velocity to the side of the wall, if he does this, he should simply jump straight.

It would be a fix because if not he does a super small gltich/bounce thing against the wall he is already on.
I leave the code, and let me know if you have any idea how i could approach this ty!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] private Rigidbody2D theRB;
[SerializeField] private float moveSpeed;
[SerializeField] private float jumpForce;
private bool hasTouchedGround;
[SerializeField] private GameObject groundPoint;
[SerializeField] private LayerMask WhatIsGround;
[SerializeField] Animator anim;
private bool isWall;
private bool hasTouchedWall;
private Vector2 initialForce;
[SerializeField] private float maxJumpSpeed;
private bool allowJump;
//setting falling down checks
private float currentHeight;
private bool isFalling;
[SerializeField] private float wallFallingSpeed;
private Material RbMaterial;
private bool isJump = false;
private float initialForceY = 5;
private bool canWallJump;
// private void Awake()
// {
// theRB.sharedMaterial.bounciness = 1;
// }
void Start()
{
isWall = false;
hasTouchedWall = false;
allowJump = false;
hasTouchedGround = true;
anim.SetBool(“touchingWall”, false);
}
void Update()
{
//check for ground;
if (Physics2D.OverlapCircle(groundPoint.transform.position, .2f, WhatIsGround) == true)
{
hasTouchedGround = true;
theRB.sharedMaterial.bounciness = 1;
}
//setting Jump
if (Input.GetButtonDown(“Jump”))
{
isJump = true;
}
//check for falling
// if (transform.position.y < currentHeight && !isJump)
// {
// isFalling = true;
// }
// if (transform.position.y > currentHeight && isJump)
// {
// isFalling = false;
// }
Debug.Log("is touching wall " + isWall);
// Debug.Log(theRB.sharedMaterial.bounciness);
// Debug.Log("can i jump? " + isJump);
//handling direction change, THIS WAS CAUSING ISSUED WITH WALL BOUNCINESS
// if (theRB.velocity.x < 0)
// {
// transform.localScale = new Vector3(-1f, 1f, 1f);
// }
// if (theRB.velocity.x > 0)
// {
// transform.localScale = new Vector3(1f, 1f, 1f);
// }
//Setting a max velocity for the Y axis
if (theRB.velocity.y > maxJumpSpeed)
{
theRB.velocity = new Vector2(theRB.velocity.x, maxJumpSpeed);
}
if (isJump)
{
if (hasTouchedGround)
{
theRB.sharedMaterial.bounciness = 1;
theRB.velocity = new Vector2(Input.GetAxisRaw(“Horizontal”) * moveSpeed, jumpForce);
isWall = false;
canWallJump = true;
}
}
//movevement sideways
if (!isWall)
{
theRB.sharedMaterial.bounciness = 1;
theRB.velocity = new Vector2(Input.GetAxisRaw(“Horizontal”) * moveSpeed, theRB.velocity.y);
if (isJump && hasTouchedGround)
{
Jump();
hasTouchedGround = false;
}
}
if (isWall)
{
theRB.velocity = new Vector2(0, wallFallingSpeed);
theRB.sharedMaterial.bounciness = 0;
StartCoroutine(fallTimer());
if (isJump && canWallJump)
{
isWall = false;
Jump();
// isFalling = false;
theRB.velocity = new Vector2(Input.GetAxisRaw(“Horizontal”) * moveSpeed, jumpForce);
canWallJump = false;
}
}
//setting animations
anim.SetFloat(“speed”, Mathf.Abs(theRB.velocity.x));
}
void FixedUpdate()
{
// setting the initial bouncing force
initialForce = new Vector2(0, 5);
theRB.AddForce(initialForce);
}
private void LateUpdate()
{
currentHeight = transform.position.y;
}
//checking for walls
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == “Walls”)
{
isWall = true;
hasTouchedWall = true;
// anim.SetBool(“touchingWall”, true);
}
}
void Jump()
{
if (isJump)
{
theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
}
isJump = false;
hasTouchedGround = false;
}
IEnumerator fallTimer()
{
yield return new WaitForSeconds(1f);
Debug.Log(“coroutine succesfull”);
isWall = false;
}
}

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

You may edit your post above.

Here is how to begin debugging your problem:

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

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 or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

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.

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://discussions.unity.com/t/839300/3

When in doubt, print it out!™

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