- Home /
Question by
drinii · Apr 04, 2016 at 06:57 AM ·
unity 2drigidbody2dplatformer
Unity2D Can't implement wall jumping !
hello, i'm trying to create a platformer similar to super meat boy, but i'm stuck trying to implement wall jumping, i looked up every thread on wall jumping but nothing helped me for my problem because i'm using velocity.y for jumping instead of AddForce, because i've implemented jump sustain(Jump higher when you press longer the jump button), here is my code, thanks anyone who helps !
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
Rigidbody2D body;
public float velocity;
public float acceleration;
public float accelerate;
public float accLimit;
public Transform groundcheck;
public float groundChkRadius;
public LayerMask whatsground;
public bool grounded;
public bool canJump;
public float JumpSustain = 0f;
public float BaseJSustain;
public float jumpHeight;
float Hor;
void Start () {
body = GetComponent<Rigidbody2D>();
BaseJSustain = JumpSustain;
}
void Update () {
Hor = Input.GetAxis("Horizontal");
grounded = Physics2D.OverlapCircle(groundcheck.transform.position, groundChkRadius, whatsground);
// Wall Jumping
if (Input.GetKeyDown(KeyCode.K))
body.velocity = new Vector2(Hor*50000f, jumpHeight);
// Testing DeltaTime
if (Input.GetKey(KeyCode.G))
QualitySettings.SetQualityLevel(0);
if (Input.GetKey(KeyCode.F))
QualitySettings.SetQualityLevel(5);
// Jumping and Sustain Jumping
if (Input.GetKeyDown(KeyCode.Space))
{
if (grounded)
{
body.velocity = new Vector2(body.velocity.x, jumpHeight);
}
}
if (Input.GetKey(KeyCode.Space))
{
if (grounded)
{
JumpSustain = BaseJSustain;
canJump = true;
//body.velocity = new Vector2(body.velocity.x, jumpHeight);
}
if (!grounded && canJump)
{
if (JumpSustain > 0f)
JumpSustain -= Time.deltaTime;
else
{
JumpSustain = BaseJSustain;
canJump = false;
}
body.velocity = new Vector2(body.velocity.x, jumpHeight);
}
}
else
canJump = false;
// Movement
if (Mathf.Abs(Hor) > 0f)
{
if (acceleration < accLimit)
acceleration += accelerate * Time.deltaTime;
}
else if (acceleration > 1f)
acceleration -= accelerate * Time.deltaTime;
body.velocity = new Vector2((acceleration*velocity)*Hor, body.velocity.y);
}
}
Comment