- Home /
I cant get wall jumping to work in my 2D platformer (C#)
I coded this script in C#
I've pasted below the code for my entire player controller. Jumping and double jumping works fine but whenever i wall jump it just go's up the wall but doesn't push away from it. I am trying to get wall jumping similar to supermeatboy.
I thought it had something to do with the "moveVelocity = 0f;" which would just stop the player in mid air but i don't think that is the only problem because i put that in a "If (WallJumped == false)" so that if you did wall jump then it shouldn't stop your velocity. Any help would be greatly appreciated thanks.
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private float moveVelocity;
public float jumpHeight;
public float awayHeight;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
public Transform wallCheck;
public float wallCheckRadius;
public LayerMask whatIsWall;
private bool onWall;
private bool doubleJumped;
private Animator anim;
private PlayerController player;
//public GameObject respawnParticle;
private Rigidbody2D rigid;
private bool wallJumped;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
player = FindObjectOfType<PlayerController>();
player.GetComponent<Renderer>().enabled = true;
//Instantiate (respawnParticle, player.transform.position, player.transform.rotation);
rigid = GetComponent<Rigidbody2D>();
wallJumped = false;
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
onWall = Physics2D.OverlapCircle(wallCheck.position, wallCheckRadius, whatIsWall);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W) && grounded)
{
Jump();
}
if (Input.GetKeyDown(KeyCode.W) && !doubleJumped && !grounded)
{
Jump();
doubleJumped = true;
}
if (grounded)
{
doubleJumped = false;
}
anim.SetBool("Grounded", grounded);
if (onWall)
{
if (wallJumped == false)
{
doubleJumped = false;
}
else
{
doubleJumped = true;
}
if (Input.GetKeyDown(KeyCode.W) && doubleJumped == false)
{
JumpAwayFromWall();
wallJumped = true;
}
if (rigid.velocity.y < 0)
{
GetComponent<Rigidbody2D>().gravityScale = 2f;
}
else
{
GetComponent<Rigidbody2D>().gravityScale = 3.5f;
}
}
else
{
GetComponent<Rigidbody2D>().gravityScale = 3.5f;
wallJumped = false;
}
anim.SetBool("OnWall", onWall);
moveVelocity = 0f;
anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
if (GetComponent<Rigidbody2D>().velocity.x > 0)
transform.localScale = new Vector3(0.25f, 0.25f, 1f);
else if (GetComponent<Rigidbody2D>().velocity.x < 0)
transform.localScale = new Vector3(-0.25f, 0.25f, 1f);
if (Input.GetKey(KeyCode.D))
{
//GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
moveVelocity = moveSpeed;
}
if (Input.GetKey(KeyCode.A))
{
//GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
moveVelocity = -moveSpeed;
}
GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);
}
public void Jump()
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
}
public void JumpAwayFromWall()
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(awayHeight, jumpHeight));
//GetComponent<Rigidbody2D>().velocity = new Vector2(awayHeight, jumpHeight);
//GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.y, jumpHeight);
//GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
//GetComponent<Rigidbody2D> ().velocity = new Vector2(-knockback, knockback);
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.transform.tag == "MovingPlatform")
{
transform.parent = other.transform;
}
}
void OnCollisionExit2D(Collision2D other)
{
if (other.transform.tag == "MovingPlatform")
{
transform.parent = null;
}
}
private void Flip()
{
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Probably you have just to change the direction of your horizontal force. If the character runs towards the right side, it should be negative to push away to the left.
You can either add an if to change the value depending on the direction you are facing or use AddRelativeForce()
(although I don't know if setting the scale.x is actually flipping the x-axis of the transform as well).
GetComponent<Rigidbody2D>().AddForce(new Vector2((transform.localScale.x < 0 ? -1 : 1) * awayHeight, jumpHeight));
Also you might have to use a different Force$$anonymous$$ode or a higher horizontal force if the player has still some horizontal velocity that works against it.
Answer by CnrGames · Aug 01, 2016 at 04:01 AM
lso you might have to use a different ForceMode or a higher horizontal force if the player has still some horizontal velocity that works against it.