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 AnthonySturdy · Oct 06, 2014 at 01:09 AM · 2dwall jump

2D Wall Jump

I watched the Live Training 16 video which covers 2D character controller. I've figured out some things by myself such as stopping the character sticking to the walls but what I would like is when you hit the wall, the jump resets and you can jump off and it gives you a small boost. I'm quite new to coding so sorry if it's stupidly obvious what to do.

Here is my code so far:

 using UnityEngine;
 using System.Collections;
 
 public class ControllerScript : MonoBehaviour 
 {
     
     public float maxSpeed = 10f;
     bool facingRight = true;
     
     Animator anim;
     
     bool grounded = false;
     public Transform groundCheck;
     float groundRadius = 0.2f;
     public LayerMask whatIsGround;
     public float jumpForce = 700f;
     
     bool doubleJump = false;
     
     
     // Use this for initialization
     void Start () 
     {
         anim = GetComponent<Animator> ();    
     }
     
     // Update is called once per frame
     void FixedUpdate () 
     {
         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");
         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
         
         anim.SetFloat ("Speed", Mathf.Abs (move));
         
         if (move > 0 && !facingRight)
             Flip ();
         else if (move < 0 && facingRight)
             Flip ();
     }
     
     void Update()
     {
         if((grounded || !doubleJump) && Input.GetKeyDown(KeyCode.Space))
         {
             anim.SetBool("Ground", false);
             rigidbody2D.AddForce(new Vector2(0, jumpForce));
             
             if(!doubleJump && !grounded)
                 doubleJump = true;
         }
     }
     
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

Thanks :)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by alap soni · Oct 06, 2014 at 08:37 AM

For jump you can use : Add rigid body to your gameobject.

transform.rigidbody.AddForce(Vector3.up*2f);

This is my code to add small force when jump.

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
avatar image
0

Answer by Austinh1 · Jan 09, 2015 at 05:43 AM

Hey! I know I'm erecting this thread from the dead, but I'll post here anyway since this is the thread that comes up when you type this question into google.

I've been working on the wall jump problem all night, and I also watched the Live Training video. I think I've finally got it right, but it took some work and is a bit conceited.

Take a look!

 using UnityEngine;
 using System.Collections;
 
 public class PlayerContoller : MonoBehaviour 
 {
     public float maxSpeed = 10f;
     public float jumpForce = 4f;
     public float jumpPushForce = 10f;
     bool facingRight = true;
     bool doubleJump = true;
     bool wallJumped = false;
     bool wallJumping = false;
 
     bool grounded;
     bool touchingWall;
     float checkRadius = 0.2f;
     public Transform groundCheck;
     public Transform wallCheck;
     public LayerMask whatIsGround;
 
     void Start () 
     {
     
     }
     
     void FixedUpdate () 
     {
         grounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
         touchingWall = Physics2D.OverlapCircle(wallCheck.position, checkRadius, whatIsGround);
         float move = Input.GetAxis("Horizontal"); 
 
         if (grounded)
             rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
         else if((move != 0 && !wallJumping))
             rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
 
 
 
         if(wallJumped)
         {
             rigidbody2D.velocity = new Vector2(jumpPushForce * (facingRight ? -1:1), jumpForce);
             wallJumping = true;
             wallJumped = false;
         }
 
         if(rigidbody2D.velocity.y < 0)
         {
             Debug.Log("Falling!");
             wallJumping = false;
         }
         if (grounded)
         {
             doubleJump = true;
             wallJumping = false;
         }
         if (rigidbody2D.velocity.x > 0 && !facingRight)
         {
             Flip();
         }
         else if (rigidbody2D.velocity.x < 0 && facingRight)
         {
             Flip();
         }
     }
 
     void Update()
     {
         bool jump = Input.GetButtonDown("Jump");
         if (jump)
         {
             if (grounded)
             {
                 Debug.Log("Jump!");
                 rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpForce);
             }
             else if (touchingWall)
             {
                 Debug.Log("Wall jump!");
                 wallJumped = true;
             }
             else if (doubleJump)
             {
                 Debug.Log("Double jump!");
                 rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpForce / 1.1f);
                 doubleJump = false;
             }
         } 
     }
 
     void Flip()
     {      
         facingRight = !facingRight;
 
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

The only thing you'll have to do outside of this code is obviously make an object for the "touchingWall" transform variable. Other than that, this should work. Do note that after completing a wall jump, you cannot change your direction until you are falling. This might turn out to be a crutch, but at the very least it works.

If anyone needs an explanation on how my code works, just let me know and I'll be happy to help.

Comment
Add comment · Show 1 · 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 IITimboyII · Apr 20, 2018 at 08:34 PM 0
Share

How do you make a transform variable for "touchingWall"?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

I cant get wall jumping to work in my 2D platformer (C#) 1 Answer

[Completed] how to make you not be able to bounce off the same wall 0 Answers

How to make your 2D character Wall Jump? 2 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 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