• 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
Question by Austinh1 · Jan 11, 2015 at 06:05 PM · jumpwallwalljump

Unity2D Platformer - Wall jumping with unity's physics

Hello!

I'm fairly new to Unity, as I've only worked on one other project besides this one. However, I'm not new to C# nor coding in general.

Basically my problem is, is that when I wall jump from one wall to another, the player's rigidbody doesn't fully come into contact with the wall, and what's more the player kinda bounces off of the wall, so the player can't wall jump from the wall. There is a physics material on the wall, but its values are both zero for friction and bounce. I'm also using add force to allow the player to move in the air, and I'm assigning velocity to jump, double jump and wall jump. I do however have two colliders on my character, one circle and one box. If anyone needs a video/gif of this occuring, I'd be happy to. I'd like to have the player be able to wall jump between walls until they get to the top.

Is there anyway to make a rigidbody2D come in full contact with a wall?

Here's my code, though it may be a tad confusing to read:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerContoller : MonoBehaviour
 {
     public float walkSpeed = 25f;
     public float runSpeed = 45f;
     public float pushForce = 150f; //the amount of pull you have in the air
     public float jumpForce = 50f;
     public float wallJumpPushForce = 40f;
     public float wallJumpForce = 40f;
     public float fastFallFactor = 40f;
     float jumpSpeed;
     float pushForceLimiter; //Limits how fast you can move in the air with AddForce()
     float normalGravity;
     float direction;
     float move;
     bool running;
     bool facingRight = true;
     bool jumped = false; //Has no function at the moment
     bool doubleJump = true;
     bool superJump = true; //Has no function at the moment
     bool wallJumped = false;
     bool fastFalling = false;
 
     bool grounded;
     bool touchingWall;
     bool touchedWall;
     float checkRadius = 0.2f;
     public Transform groundCheck;
     public Transform wallCheck;
     public LayerMask whatIsGround;
 
     void Start()
     {
         normalGravity = rigidbody2D.gravityScale; //This value is 15.
         pushForceLimiter = walkSpeed;
     }
 
     void FixedUpdate()
     {
         direction = Input.GetAxis("Horizontal");
         running = Input.GetButton("Run");
 
         grounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
         touchingWall = Physics2D.OverlapCircle(wallCheck.position, checkRadius, whatIsGround);
 
         move = direction * (((running ? 1 : 0) * runSpeed) + ((running ? 0 : 1) * walkSpeed));
 
         if (grounded)
         {
             rigidbody2D.velocity = new Vector2(move, rigidbody2D.velocity.y);
         }
         else
         {
             rigidbody2D.AddForce(new Vector2(direction * pushForce, 0));
             if (Mathf.Abs(rigidbody2D.velocity.x) > pushForceLimiter)
             {
                 rigidbody2D.velocity = new Vector2(direction * pushForceLimiter, rigidbody2D.velocity.y);
             }
         }
 
         if (fastFalling)
             rigidbody2D.gravityScale = normalGravity * fastFallFactor;
 
         if (grounded)
         {
             doubleJump = true;
             jumped = false;
             superJump = true;
             wallJumped = false;
             pushForceLimiter = ((running ? 1 : 0) * runSpeed) + ((running ? 0 : 1) * walkSpeed);
             if (fastFalling)
             {
                 fastFalling = false;
                 rigidbody2D.gravityScale = normalGravity;
             }
         }
         else if(touchingWall && !touchedWall) //An attempt to solve the problem, but it didn't work. 
         {
             if (!wallJumped)
             {
                 rigidbody2D.velocity = new Vector2(0, rigidbody2D.velocity.y);
                 touchedWall = true;
             }
             else wallJumped = false;
 
         }
 
         
 
         if (rigidbody2D.velocity.x > 0 && !facingRight)
             Flip();
         else if (rigidbody2D.velocity.x < 0 && facingRight)
             Flip();
     }
 
     void Update()
     {
 
         bool jump = Input.GetButtonDown("Jump");
         bool fall = Input.GetButtonDown("Fall");
 
         if (jump)
         {
 
             if (grounded)
             {
                 Debug.Log("Jump!");
                 jumped = true;
                 rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpForce);
             }
             else if (touchingWall)
             {
                 Debug.Log("Wall jump!");
                 pushForceLimiter = wallJumpPushForce;
                 //These next two variables are ones I made specifically to solve this issue. 
                 touchedWall = false;
                 wallJumped = true;
                 rigidbody2D.velocity = new Vector2(wallJumpPushForce * (facingRight ? -1 : 1), wallJumpForce);
             }
             else if (doubleJump)
             {
                 Debug.Log("Double jump!");
                 rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpForce / 1.1f);
                 doubleJump = false;
             }
         }
 
         if (fall)
             if (rigidbody2D.velocity.y > -20 && rigidbody2D.velocity.y < 0)
                 fastFalling = true;
 
     }
 
     void Flip()
     {
         facingRight = !facingRight;
 
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 
     //Ignore this. Faild attempt at a solution.
     void OnCollisionEnter2D(Collision2D collision) {
         if(collision.collider.tag == "Block")
         {
             if (!grounded)
             {
                 rigidbody2D.velocity = new Vector2(0, 0);
                 Debug.Log("Hit Wall!");
             }
         }
   }
 }

Thanks for any insight that you all can provide me!

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by kupot · May 04, 2015 at 11:23 AM

I ran into a similar problem myself using the Physics2D.OverlapCircle function. Instead, using Raycasts you can do a whole lot more. I would suggest looking into using Raycasts for ground and wall detection - that way if the players rigidbody doesnt touch the wall (as you're experiencing) the raycast will be able to detect your proximity to the wall and take action when the wall is a certain distance from your player object.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Making a character jump with the Character Controller Component instead of Capsule Collider 0 Answers

3d wall jumping with charter controller 1 Answer

HELP! stuck on wall jump Unity 2D 1 Answer

Jump and Hold on!! 0 Answers

Wall jump need help!!!!! 1 Answer


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