- Home /
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;
}
}
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.
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,
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.
http://answers.unity3d.com/questions/585877/rigidbody-sticks-to-walls.html
Could your problem relate to this topic?
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 :)
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)
Your answer
Follow this Question
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