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 Rodney26 · Apr 09, 2015 at 10:37 PM · jumpwallwall jump

HELP! stuck on wall jump Unity 2D

Okay let me start of with the normal:

Im new to Unity, I have only been coding in C# for about a week, I apologize if my code looks messy.

I am having the right bollock ache with getting my little 2D guy jumping off of a wall.

the Y axis is fine... the player jumps according to what number I decide should be the force for jumping up when against a wall

but...

for the life of me i can not get him to jumpBackwards away from the wall

here is my code...

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 // moving 
 public float MaxSpeed = 10f;
 private bool faceRight = true;

 //Jumping

 public float airControl = 0.5f;
 public float JumpForce = 300f;
 public float doubleJumpForce = 2f;
 private float maxJumpTime = 0.1f;
 public int jumpNumber; 
 public int totalJump = 2; 
 private float timer;
 private bool canJump;

 // What is Wall / Wall Jump

 public bool TouchingWall = false;

 public Transform WallCheck;
 public LayerMask whatIsWall;
 private float WallRadius = 1f;

 public int PushForceRight;
 public int PushForceLeft;
 public float WallJumpForce;

 // What is Ground
  
 private float groundRadius = 0.2f;
 public bool Grounded = false;
 public Transform groundCheck;
 public LayerMask whatIsGround;
 public bool touchingWallLeft = false;
 public bool touchingWallRight = false;

 // crouch

 private bool crouch = false;

 // Spawning

 public Transform target;

 // Animator

 Animator anim;


 void Start () {
 
     anim = GetComponent<Animator>();

 }
 

 void FixedUpdate () {

     Grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
     anim.SetBool ("Ground", Grounded);

     anim.SetBool ("Crouch", crouch);

     // Wall Jump

     TouchingWall = Physics2D.OverlapCircle (WallCheck.position, WallRadius, whatIsWall);

         

     // moving

     float move = Input.GetAxis ("Horizontal");
     anim.SetFloat ("Speed", Mathf.Abs (move));

     // When On The Ground

     if (Grounded) {
         canJump = true; 
         jumpNumber = totalJump;
         touchingWallLeft = false;
         touchingWallRight = false;
     
     }



     if (Grounded) { // movement on the floor
         GetComponent<Rigidbody2D> ().velocity = new Vector2 (move * MaxSpeed, GetComponent<Rigidbody2D> ().velocity.y);
     } else { // movement in the air
         GetComponent<Rigidbody2D> ().velocity = new Vector2 (move * MaxSpeed * airControl, GetComponent<Rigidbody2D> ().velocity.y);
     }
 

     if (move > 0 && !faceRight) // flips the direction of character
         Flip ();
     else if (move < 0 && faceRight)
         Flip ();
 
     }
 

 void Update(){

      if (Grounded && Input.GetKeyDown (KeyCode.Space)) { // Jump
         timer = 0;
         canJump = true;
         GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, JumpForce));
         
     } else if (Input.GetKey (KeyCode.Space) && canJump && timer < maxJumpTime) { // Jump Higher when pressed longer

         timer += Time.deltaTime;
         GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, JumpForce));

     } else if (Input.GetKeyDown (KeyCode.Space) && !Grounded && jumpNumber>0) { // Double Jump
             
         jumpNumber--;
         timer = 0;
         canJump = true;
         GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, doubleJumpForce);
     }else {
     }canJump = false;

// HERE IS WHERE MY ISSUE IS *

     if (TouchingWall && faceRight) {
         touchingWallRight = true;
         touchingWallLeft = false;

     

         if (Input.GetKeyDown (KeyCode.Space) && touchingWallRight) {
             GetComponent<Rigidbody2D> ().velocity = new Vector2 (PushForceRight * 1, WallJumpForce); // WallJumpForce works great but pushForceRight does nothing... even when i change the 1 to -1
         }
     }

         if (TouchingWall && !faceRight) {
         touchingWallLeft = true;
         touchingWallRight = false;

     

     if (Input.GetKeyDown (KeyCode.Space)&& touchingWallLeft) {
         GetComponent<Rigidbody2D> ().velocity = new Vector2 (PushForceLeft * -1, WallJumpForce);
     }
 }

// *

     if (Input.GetKey ("down")) { // crouch when down pressed
         crouch = true;
         GetComponent<BoxCollider2D>().enabled = false; // disables box collider 

     } else{
         crouch = false;
         GetComponent<BoxCollider2D>().enabled = true;

     }


     if(Input.GetKeyDown (KeyCode.W)) {
         this.transform.position = target.position; // respawn character
     }



 }
 

 void OnTriggerEnter2D(Collider2D collider)
 {

     HitByLaser(collider);
 }
 
 void HitByLaser(Collider2D laserCollider)
 {
     if(laserCollider.gameObject.tag == "Enemy") { // when player hits Enemy respawn
         this.transform.position = target.position;
     }

 }
 
 void Flip(){ // flips direction of character
     faceRight = !faceRight;
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;
 }

}

Comment
Add comment · Show 6
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 Rodney26 · Apr 09, 2015 at 08:40 PM 0
Share

Also the bools touchingWallLeft and touchingWallRight both work fine as i can see them in the inspector when the player touches the correct side of the wall.

avatar image AlwaysSunny · Apr 09, 2015 at 09:07 PM 1
Share

You will seriously improve your chances of getting your code proofread if you trim the fat. Ditch the "HELP!" too. ;)

It can be hard to know what's essential and what's not, but the "here's the problem" part might've been sufficient.

If you're sure the pushForceWhatever variables are non-zero, I would suggest that you're most likely changing the velocity again somewhere else. This logic looks a bit tangled; you'll write stronger, cleaner code as you pick up experience. $$anonymous$$y guess is, at some point after this executes, you're altering the x velocity again.

Generally when I need several pieces of code to change one variable (like a velocity) I perform an additive operation. In this case, a temporary Vector2 (vel) you manipulate as needed through your logic chain. This allows you to use a single call to whatever makes use of that variable (assigning it to the rigidbody's velocity) at the end of the chain. This is wise for several reasons, including making problems easier to track down.

 Vector2 vel = Vector2.zero;
 if (something) vel.x += 5;
 if (somethingElse) vel.x += 10;
 if (yetAnotherThing) vel.x -= 2;
 body.velocity = vel;


Best,

avatar image KitoCode · Apr 10, 2015 at 02:27 AM 1
Share

I just skimmed through your question, and I'll take a wack at this, but are you using a rigid body? Try adding a Physics$$anonymous$$aterial to the wall with 0 friction.

avatar image KitoCode · Apr 10, 2015 at 02:28 AM 0
Share

http://answers.unity3d.com/questions/585877/rigidbody-sticks-to-walls.html

Could your problem relate to this topic?

avatar image Rodney26 · Apr 11, 2015 at 03:08 PM 0
Share

Thanks to everyone who has helped me out in this question of $$anonymous$$e

to @AlwaysSunny firstly I will ditch the HELP! from now on... i was just getting properly frustrated with wall jumping I raged at the keyboard when typing, also 'I would suggest that you're most likely changing the velocity again somewhere else' was really helpful i didnt even think about that and looking back through my code i found that when ever the player was in not on the ground then the velocity for the X axis was overriding anything else i wrote for it. and thank you for writing a little bit of code for me, i can see your logic and it makes alot more sense doing it that way and is a style i am going to adopt.

to @IcarusOtaku im already ahead of you there man, i had the friction material on the walls but i found that going for 0 makes him slide straight off so i went for 0.001 for they look i was after but thanks anyways :)

and finally to @Good_Rabbi i am going to attempt to use add force ins$$anonymous$$d of using velocity... you sound like you know what you are on about so I will go for the more trusty method, thank you especially for showing me a code snippet of addforce because every way i have done it before it had a new vector 2 which was slightly confusing me.

Thanks to you all... its working a lot better now... not perfect but i no longer hate unity and have stopped punching my computer monitor :)

Show more comments

1 Reply

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

Answer by Good_Rabbi · Apr 10, 2015 at 02:36 AM

Right off the bat, it seems you are attempting to change the rigidbody velocity with code. As you have just begun your trip into the world of Unity. I can tell you that that is not a great practice... Since you are moving a body in "physical space", the more trusty method would be to apply force to your object using Rigidbody.Addforce(). I will not go into detail explaining it currently, as there is MUCH documentation.

When you perform your wall checks and input checks, you could call rigidbody.AddForce(PushForce, Forcemode.Impulse); This would apply your force in an instant. Also I would try experimenting with adding another multiplier into your Addforce(), for debugging, so you can see if the force is being applied but only a teensy bit. Something like ... rigidbody.AddForce(PushForce*multiplier, Forcemode.Impulse); Where "multiplier" is a public variable(so you can view and tweak it at runtime)high number.(I would start at 100 at least)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Jump and Hold on!! 0 Answers

Jumping away from a wall 1 Answer

3d wall jumping with charter controller 1 Answer

Wall jump need help!!!!! 1 Answer

I'm trying to make a 3D player do a wall jump. Does anyone have advice? 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