- Home /
Wall jump push force
I followed a tutorial about implementing a wall jump but one thing that doesn't work is getting the character pushed on the opposite side of the wall when pressing the jump button. As it is, character is only jumping on the y axis upwards on the wall. I read a possible solution that my move code in the FixedUpdate is overriding my wall jumping code. I tried to set a condition to run the move code only if isWallJumping bool is false but to no avail. I have no idea what to try next.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class PlayerController : MonoBehaviour {
public Animator animator;
public Rigidbody2D rb;
public float speed;
private float moveInput;
public float jumpForce;
public float maxFallVelocity = -20;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
IEnumerator dashCoroutine;
bool isDashing;
bool canDash = true;
float normalGravity;
bool isTouchingFront;
public Transform frontCheck;
bool wallSliding;
public float wallSlidingSpeed;
public bool wallJumping;
public float xWallForce;
public float yWallForce;
public float wallJumpTime;
private bool facingRight = true;
void Start()
{
rb = GetComponent<Rigidbody2D>();
normalGravity = rb.gravityScale;
}
void Update()
{
Debug.Log("The velocity in the Y-axis is: " + rb.velocity.y);
isGrounded = Physics2D.OverlapCircle(feetPos.position, .2f, whatIsGround);
//Flipping
if (moveInput > 0 && facingRight == false)
{
Flipper();
}
else if (moveInput < 0 && facingRight == true)
{
Flipper();
}
//////////////////////////////////////////////////////////
//Animation
/////////////////////////////////////////////////////////
if (Input.GetButtonDown("Jump") && rb.velocity.y == 0)
rb.AddForce(Vector2.up * 700f);
if (Mathf.Abs(moveInput) > 0 && rb.velocity.y == 0)
animator.SetBool("Running", true);
else
animator.SetBool("Running", false);
if (rb.velocity.y == 0)
{
animator.SetBool("Jumping", false);
animator.SetBool("Falling", false);
}
if (rb.velocity.y > 0.2)
{
animator.SetBool("Jumping", true);
}
if (rb.velocity.y < 0)
{
animator.SetBool("Jumping", false);
animator.SetBool("Falling", true);
}
if (isDashing && moveInput > 0)
{
animator.SetBool("Dashing", true);
}
else if (isDashing && moveInput < 0)
{
animator.SetBool("Dashing", true);
}
else
{
animator.SetBool("Dashing", false);
}
///////////////////////////////////////////////////////
// Jumping
//////////////////////////////////////////////////////
if (isGrounded == true && Input.GetButtonDown("Jump"))
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if (Input.GetButton("Jump") && isJumping == true)
{
if (jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
{
isJumping = false;
rb.velocity = new Vector2(rb.velocity.x, 0f);
}
/////////////////////////////////////////////////////////
// DASHING
if (Input.GetButtonDown("Dash") && canDash == true)
{
if (dashCoroutine != null)
{
StopCoroutine(dashCoroutine);
}
dashCoroutine = Dash(.4f, .5f);
StartCoroutine(dashCoroutine);
}
////////////////////////////////////////////////////////
// Wall Jumping
isTouchingFront = Physics2D.OverlapCircle(frontCheck.position, .2f, whatIsGround);
if (isTouchingFront == true && isGrounded == false && moveInput != 0)
{
wallSliding = true;
}
else
{
wallSliding = false;
}
if (wallSliding)
{
rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
if (Input.GetButtonDown("Jump") && wallSliding == true)
{
wallJumping = true;
Invoke("SetWallJumpingToFalse", wallJumpTime);
}
if (wallJumping == true)
{
rb.velocity = new Vector2(xWallForce * -moveInput, yWallForce);
}
}
private void FixedUpdate()
{
moveInput = Input.GetAxisRaw("Horizontal");
if (wallJumping == false)
{
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
///////////////////////////////////////////////////////////////////////
///DASHING
//////////////////////////////////////////////////////////////////////
if (isDashing && moveInput > 0)
{
rb.AddForce(new Vector2(1f * 130, 0), ForceMode2D.Impulse);
}
if (isDashing && moveInput < 0)
{
rb.AddForce(new Vector2(1f * -130, 0), ForceMode2D.Impulse);
}
}
//DASHING SCIENCE FICTION
IEnumerator Dash(float dashDuration, float dashCooldown)
{
Vector2 originalVelocity = rb.velocity;
isDashing = true;
canDash = false;
rb.gravityScale = 0;
rb.velocity = new Vector2(originalVelocity.x, 0);
yield return new WaitForSeconds(dashDuration);
isDashing = false;
rb.gravityScale = normalGravity;
rb.velocity = new Vector2(originalVelocity.x, 0);
yield return new WaitForSeconds(dashCooldown);
canDash = true;
}
void Flipper()
{
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
facingRight = !facingRight;
}
void SetWallJumpingToFalse()
{
wallJumping = false;
}
}
You need to add force in the direction opposite of the wall. What you can do is when you do a wall jump detect if you are against the wall and then addforce in the opposite direction.
I believe this line of code in line 182:
rb.velocity = new Vector2(xWallForce * -moveInput, yWallForce);
should propel me in the opposite direction, but it has been noted to me that moveInput is being overriden by this on line 194 in FixedUpdate:
moveInput = Input.GetAxisRaw("Horizontal"); if (wallJumping == false) { rb.velocity = new Vector2(moveInput * speed, rb.velocity.y); }
i tried enclosing it in an if statement but still doesn't work. I have no idea what else to try. Btw I tried the AddForce method on the x axis and still doesn't work. The problem is not the force with which it jumps, because the character will use his own jumpforce. The problem is the direction in which it jumps. Thank you!
Answer by Purpleomen · Feb 26, 2021 at 10:40 PM
I partially fixed the problem by removing the need to move in other to wall Cling: In line 161 I changed:
if (isTouchingFront == true && isGrounded == false && moveInput != 0)
to
if (isTouchingFront == true && isGrounded == false)
thus removing the need to move left or right in order to cling to a wall.
Your answer
Follow this Question
Related Questions
2D Wall-Jump, preventing jumps/sticking on the same wall. 0 Answers
Use 2D Effectors with Raycast Collision Method 0 Answers
I have a problem with Layermask Unity2D 0 Answers
How solve the 2d collider penetration problem ? 2 Answers
[2Dplatformer][Problem] When the player closes to the enemy, the enemy pass through the colliders 0 Answers