Wall jump help

I am trying to make my character wall jump diagonally and right now they only jump vertically and one in ten times they instantly snap a little to the right here is my code so far
private void WallJump()
{
if (!myCapsuleCollider2D.IsTouchingLayers(LayerMask.GetMask(“Ground”)) == false && !myBoxCollider2D.IsTouchingLayers(LayerMask.GetMask(“Ground”)) == true)
{
bool wallJumping = CrossPlatformInputManager.GetButtonDown(“Jump”);

            if (wallJumping)
            {
                Vector2 jumpVelocity = new Vector2(10f, wallJumpHeight);
                myRigidBody2D.velocity = jumpVelocity;

               
            }
        }
    }

right now I am just trying to get it to jump diagonally, I will get it to jump off in the right direction of the wall later. I have searched google everywhere and nothing has helped it would be really nice if somebody could help me. The wallJumpHeight is a serialized field by the way.

Is it fine if you use AddForce()? That way, you can add an upwards force when wall jumping, and a right force, which creates a diagonal jump. To get the right force, you need the normal of the wall you’re jumping off of.


One way to do this is by using a raycast. When walljumping, fire a raycast from the player to its forward, back, left and right directions (with a range of like 1) and whichever hits a wall, get that normal, and apply a force in that direction to the player. For example:

public GameObject Player;
Rigidbody rb;

public float UpForce;
public float PushForce;

void Start()
{
    rb = Player.GetComponent<Rigidbody>();
}

void Update()
{
    if (wallJumping)
    {
        WallJump();
    }
}

void WallJump()
{
    // Looks for direction of wall
    Vector3[] directions = new Vector3[4]
    {
            Player.transform.forward,
            -Player.transform.forward,
            Player.transform.right,
            -Player.transform.right
    };

    foreach (var dir in directions)
    {
        if (Physics.Raycast(Player.transform.position, dir, out RaycastHit hit, 1f))
        {
            // Wall jump forces
            PlayerRb.AddForce(Vector3.up * UpForce);
            PlayerRb.AddForce(hit.normal * PushForce);
        }
    }
}

@Nubby3050