Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Purpleomen · Feb 23, 2021 at 09:04 AM · 2d-platformer2d-physicswall jumpwalljump

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;
     }
 }
 

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image logicandchaos · Feb 23, 2021 at 01:31 PM 0
Share

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.

avatar image Purpleomen logicandchaos · Feb 23, 2021 at 05:41 PM 0
Share

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!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges