Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by sayedstafa · Feb 20, 2014 at 10:55 AM · unity 2d2d-platformer2d-physicswall collision

2D Wall Jump and Wall Friction

EDIT: Wall Friction is taken care of. Just need Wall-Jumping now

First off, I want to say I'm very novice when it comes to coding. So I'm going to need some guidance here with these two issues.

I watched the Live Training 16 video which covers 2D character controller. Here's the Link. So I got the character moving, got double jump working. I two questions. First is an issue with sticking to walls. If I am holding a direction, jump at a wall, and hold that direction, my character will stick to the wall and not slide down. I'm searching the web for an answer, and I'm supposed to do something with no friction.

My second thing is, using this code, how do I add wall-jumping?

Here's the code that I have, which is from that video I linked above. If someone can help me out with this, please be specific with step-by-step instructions. Thanks in advance!

 using UnityEngine;
 using System.Collections;
 
 public class CharacterControllerScript : MonoBehaviour {
 
     public float maxSpeed = 10f;
     bool facingRight = true;
 
     Animator anim;
 
     //sets up the grounded stuff
     bool grounded = false;
     public Transform groundCheck;
     float groundRadius = 0.2f;
     public LayerMask whatIsGround;
     public float jumpForce = 700f;
 
     //double jump
     bool doubleJump = false;
 
     // Use this for initialization
     void Start () {
 
         anim = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void FixedUpdate () {
 
         // The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
         grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
         anim.SetBool("Ground", grounded);
 
         if (grounded) {
             doubleJump = false;
         }
 
         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
 
 
 
         float move = Input.GetAxis ("Horizontal");
 
         anim.SetFloat("Speed", Mathf.Abs (move));
 
         rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
 
         // If the input is moving the player right and the player is facing left...
         if(move > 0 &&!facingRight){
             // ... flip the player.
             Flip ();
         }// Otherwise if the input is moving the player left and the player is facing right...
         else if(move < 0 && facingRight){
             // ... flip the player.
             Flip ();
         }
     }
     void Update(){
 
         // If the jump button is pressed and the player is grounded then the player should jump.
         if((grounded || !doubleJump) && Input.GetButtonDown("Jump")){
             anim.SetBool("Ground", false);
             rigidbody2D.AddForce(new Vector2(0, jumpForce));
 
             if(!doubleJump && !grounded){
                 doubleJump = true;
             }
         }
 
     }
 
 
     void Flip(){
 
         // Switch the way the player is labelled as facing
         facingRight = !facingRight;
 
         //Multiply the player's x local cale by -1
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }
 


Comment
Add comment · Show 1
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 Norbertof · Nov 16, 2014 at 02:55 AM 0
Share

Thank you guy this really help my out too!

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by DricoJD · Feb 20, 2014 at 11:43 AM

Okay, here is an answer to your wall sticking:

Step One:

Right click in your Project window and create a new physics material, then put the settings to that there is no friction.

Step two:

Go to the colliders of your walls and assign it that way.

Now to make your character wall jump:

Copy the GameObject groundcheck in Unity. and put it to the middle of the player and just right of the player. Call it "Wall Check"

Now, and please add this script, it is an edited version of your script so just copy and paste over your whole script.

 using UnityEngine;
 using System.Collections;
 
 public class CharacterControllerScript : MonoBehaviour {
     
     public float maxSpeed = 10f;
     bool facingRight = true;
     
     Animator anim;
     
     //sets up the grounded stuff
     bool grounded = false;
     bool touchingWall = false; 
     public Transform groundCheck;
     public Transform wallCheck;
     float groundRadius = 0.2f;
     float wallTouchRadius = 0.2f;
     public LayerMask whatIsGround;
     public LayerMask whatIsWall;
     public float jumpForce = 700f;
     public float jumpPushForce = 10f;
     
     //double jump
     bool doubleJump = false;
     
     // Use this for initialization
     void Start () {
         
         anim = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         
         // The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
         grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
         touchingWall = Physics2D.OverlapCircle(wallCheck.position, wallTouchRadius, whatIsWall);
         anim.SetBool("Ground", grounded);
         
         if (grounded) 
         {
             doubleJump = false;
         }
 
         if (touchingWall) 
         {
             grounded = false; 
             doubleJump = false; 
         }
         
         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
         
         
         
         float move = Input.GetAxis ("Horizontal");
         
         anim.SetFloat("Speed", Mathf.Abs (move));
         
         rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
         
         // If the input is moving the player right and the player is facing left...
         if(move > 0 &&!facingRight){
             // ... flip the player.
             Flip ();
         }// Otherwise if the input is moving the player left and the player is facing right...
         else if(move < 0 && facingRight){
             // ... flip the player.
             Flip ();
         }
     }
     void Update()
     {
         
         // If the jump button is pressed and the player is grounded then the player should jump.
         if((grounded || !doubleJump) && Input.GetButtonDown("Jump"))
         {
             anim.SetBool("Ground", false);
             rigidbody2D.AddForce(new Vector2(0, jumpForce));
             
                         if(!doubleJump && !grounded)
                         {
                             doubleJump = true;
                         }
         }
 
         if (touchingWall && Input.GetButtonDown ("Jump")) 
         {
             WallJump ();
         }
         
     }
 
     void WallJump () 
     {
         rigidbody2D.AddForce (new Vector2 (jumpPushForce, jumpForce));
     }
     
     
     void Flip(){
         
         // Switch the way the player is labelled as facing
         facingRight = !facingRight;
         
         //Multiply the player's x local cale by -1
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }
 

Now I will need you to save the script go back to unity and assign the wallCheck variable in the Inspector. Also assign the other variables like WhatIsWall (LayerMask). create a Layer called "Wall" and assign your walls layers to "Wall".

Comment
Add comment · Show 6 · 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
avatar image sayedstafa · Feb 20, 2014 at 05:39 PM 0
Share

Thanks, that takes care of that issue.

avatar image DricoJD · Feb 21, 2014 at 09:46 AM 0
Share

I am gonna try \and code your script now, and see

avatar image DricoJD · Feb 21, 2014 at 10:03 AM 0
Share

All edited and done

avatar image sayedstafa · Feb 22, 2014 at 06:11 AM 0
Share

I tried out the new script, wall jumping works but sometimes the player will make one really big super jump. Do you know what might be causing this issue?

avatar image Ferb · Sep 09, 2014 at 04:22 PM 0
Share

Usually that's caused by the jump not quite taking them out of range of the ground on the first frame - they still register as grounded on the first frame of their jump, and still have jump held down, so the jumping force is applied again. To avoid that, have a set number of frames after a jump (about 5 should be fine) before the character can be considered grounded again. I've never handled wall-jumping, but I guess the same problem will be possible.

Show more comments

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

22 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

Related Questions

When Player Collide with Cube, Cube changes to Rigidbody 2D 1 Answer

OnTriggerEnter2D(Collider2D other) 2 Answers

Handling friction in scripts for a 2D platformer 1 Answer

Unity 2D player sticks on platform corners 2 Answers

Way to achieve 3 lane mechanism in a 2D game 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