- Home /
problems setting up wall jump
hey, I´m very new in unity and know almost nothing about coding. but I'm following tutorials and trying to make a game.
I had already set the movement and the jump following a tutorial, and then I tried to follow a different one to establish the wall slice and walljump and this is when the problems started.
I pass few days trying to put all the things of this new tutorial in a different script and with this new script my player wall slide perfectly but the wall jump is really not working properly.
this is the tutorial from which I was trying to learn the wall jump: https://www.youtube.com/watch?v=5ANjGwcr1hs&ab_channel=Bardent.
this is the code I now have trying to put only the wallslide and walljump to my character (because his movement is in a different script):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class wallsliding : MonoBehaviour
{
private bool isfacingright = true;
private int facingdirection = -1;
public Transform wallcheck;
public Transform feet;
public LayerMask ground;
public Animator amin;
public float walldistance;
public Rigidbody2D rb;
private bool iswallsliding;
public float wallslidespeed;
public float walljump;
public float walljumpforce;
public float wallhopforce;
public Vector2 wallhopdirection;
public Vector2 walljumpdirection;
private void Start()
{
walljumpdirection.Normalize();
wallhopdirection.Normalize();
}
private void FixedUpdate()
{
if(iswallsliding) {
if (rb.velocity.y < -wallslidespeed)
{ rb.velocity = new Vector2(rb.velocity.x, -wallslidespeed);
}
if (Input.GetKeyDown(KeyCode.Space) && !isgrownded() && iswallsliding)
{
walljum();
}
}
}
private void OnDrawGizmosSelected()
{
if (wallcheck == null)
return;
Gizmos.DrawWireSphere(wallcheck.position, walldistance);
Gizmos.DrawWireSphere(feet.position, walldistance);
}
// Update is called once per frame
void Update()
{
Checkifsliding();
amin.SetBool("istouchingwall", istouchingwall());
}
public bool istouchingwall()
{
Collider2D wallslidecheck = Physics2D.OverlapCircle(wallcheck.position, walldistance, ground);
if (wallslidecheck != null)
{
return true;
}
return false;
}
public bool isgrownded() {
Collider2D groundcheck = Physics2D.OverlapCircle(feet.position, 1f, ground);
if (groundcheck != null)
{
return true;
}
return false;
}
private void Checkifsliding()
{
if (istouchingwall() && !isgrownded() && rb.velocity.y < 0)
{
iswallsliding = true;
}
else
{
iswallsliding = false;
}
}
void walljum()
{
iswallsliding = false;
Vector2 movement = new Vector2(rb.velocity.x, walljump);
Vector2 forcetoadd = new Vector2(wallhopforce * walljumpdirection.x - facingdirection, wallhopforce * wallhopdirection.y);
rb.AddForce(forcetoadd, ForceMode2D.Impulse);
facingdirection *= -1;
isfacingright = !isfacingright;
{
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
}
it kind of emulates what it should do
if I put the walljupforce and wallhopforce values like on 20 the player will just turn away from the wall (right wall and left wall) and stop wall sliding (kind of the behavior it should have) but it doesn't jump.
and if I mess whit the wallhopdirection and walljumpdirection parameters and put them like in the 20s it kind of emulates a walljump sometimes but is very imprecise, and it happens only if my player is on the right wall, if on the left wall it will just stick and go up that same wall.
and if i put the same values that he puts on the tutorial it just does the same thing that if walljupforce and wallhopforce are on 20.
as I said I don't know much about unity or coding so maybe there is some unnecessary stuff or contradiction on that code. I just have been trying to solve this problem kind of like a puzzle for many days because of my inexperience, and that is what most resembles what I want to achieve but is really not properly working, the script achieves the wallslide and the wallslide speed pretty good, but I haven't fount a way to make him reliably jump from one wall to another.
thanks a lot to however takes the time to read this and more thanks to however are interested in helping me solve this problem.
Answer by unity_ek98vnTRplGj8Q · Jan 26, 2021 at 11:54 PM
Ok there's a lot to unpack here but a couple things jump out right away.
First, you facingdirection variable doesn't seem to be set anywhere except when you walljump. You need to make sure it is accurate every update cycle, because currently if you are just running left and right using another script then the facingdirection variable won't be updated accordingly.
Second, you seem to want to use that facing direction to apply force in the opposite direction for a wall jump. Make sure you are multiplying by the direction ->
Vector2 forcetoadd =
new Vector2(wallhopforce * walljumpdirection.x * -facingdirection,
wallhopforce * wallhopdirection.y);
instead of
Vector2 forcetoadd =
new Vector2(wallhopforce * walljumpdirection.x - facingdirection,
wallhopforce * wallhopdirection.y);
Finally, the problem that I suspect is really responsible for the walljump being inconsistant. When you do the walljump, you are using AddForce which is all well and good, but what add force does is it just modifies the velocity of the rigidbody for you. But, on the next fixed update cycle what happens? If you have any code that goes and sets the velocity of the rigidbody then it may overwrite the velocity change of the addforce. Usually with something like a wall jump you want to lock the player into that movement for a short period of time to make sure the move is actually executed, ESPECIALLY if you are using a combination of AddForce() and setting the velocity directly.
Let me know if you have any more questions. Also, if you have an IDE like Visual Studio that supports debugging then that could be very useful here. In visual studio, you can attach to unity and set breakpoints in your code to see what is being executed and what the value of variables are when the code is executed. I would start off by making sure that walljum() is ACTUALLY being called when you expect it to be. You can also add Debug.Log() statements to you code to see if the the walljum code is actually being executed.
Also, you should check for input in the update method but apply physics on the fixed update. Input may not respond properly in the fixedUpdate.