- Home /
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 :)
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.
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.
How do you make a transform variable for "touchingWall"?