- Home /
Adding a wall jump with AddForce
Hello, I'm pretty new to unity and have been following a lot of tutorials online. Here I have my movement script, and I'm currently having trouble adding a wall jump with a AddForce method. I want the wall jump to push the player away from the wall, and upwards. Can anyone help? Here's the Script:
[SerializeField] private float speed;
[SerializeField] private float jumpPower;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private LayerMask wallLayer;
private Rigidbody2D body;
private Animator anim;
private BoxCollider2D boxCollider;
private float wallJumpCooldown;
private float horizontalInput;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
boxCollider = GetComponent<BoxCollider2D>();
}
private void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
if (horizontalInput > 0.01f)
transform.localScale = Vector3.one;
else if (horizontalInput < -0.01f)
transform.localScale = new Vector3(-1, 1, 1);
anim.SetBool("Running", horizontalInput != 0);
anim.SetBool("grounded", isGrounded());
if (wallJumpCooldown < 0.2f)
{
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
if (Input.GetKey(KeyCode.Space))
Jump();
}
wallJumpCooldown = Time.deltaTime;
}
private void Jump()
{
if (isGrounded())
{
body.velocity = new Vector2(body.velocity.x, jumpPower);
anim.SetTrigger("Jump");
}
else if (onWall() && !isGrounded())
{
//Need Help here :)
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
}
private bool isGrounded()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, Vector2.down, 0.1f, groundLayer);
return raycastHit.collider != null;
}
private bool onWall()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, new Vector2(transform.localScale.x, 0), 0.1f, wallLayer);
return raycastHit.collider != null;
}
Answer by KloverGames · Nov 18, 2021 at 01:19 PM
if(player is on right side)
{
body.AddForce(-10, 5, 0, ForceMode.Impulse); //The x vector is -10 because the player is on the wall from the right side
}
You can get where the player landed on the wall by using OnCollisionEnter() and when collided with the wall, set the player's position to the spot that you landed in
I tried to do that, all I get is an error: Assets\Movement.cs(56,18): error CS1501: No overload for method 'AddForce' takes 4 arguments
I should have said that first, my bad. Yes, I'm doing a 2D game.
Your answer
![](https://koobas.hobune.stream/wayback/20220613045517im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with Wall Jump - 2D 0 Answers
Rigidbody Player AddForce issue 2 Answers
Torque applied only after force 0 Answers
How to get force applied to object during FixedUpdate 1 Answer
Get result (force & torque) of AddForceAtPosition? 2 Answers