I’m designing a platformer where the player can dash in each cardinal direction (up, down, left, right). At the moment the player is able to dash, but not in each direction. The player can only dash vertically (up, down), but not horzontally(left, right). I would be grateful for any guidance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement04 : MonoBehaviour
{
[Header("Movement Parameter")]
[SerializeField] private float speed; // make player speed an object
[SerializeField] private float jumpPower; // make player jump an object
[SerializeField] private float gameGravity; // ingame gravity
[Header("Dash")]
[SerializeField] private float dashDistance; // distance that is traveled over dash
private bool isDashing; // if player is currently dashing
private bool dashCounter; // if player is able to dash
[Header("Wall Jumps")]
[SerializeField] private float wallJumpHeight; //veritcal distance
[SerializeField] private float wallJumpDistance; // horizontal distance
[Header("Multipe Jumps")]
[SerializeField] private int extraJumps; // how many extra jumps the player gets
private int jumpCounter; // how many times player has jumped
[Header("Layers")]
[SerializeField] private LayerMask groundLayer; // make groundLayer an object
[SerializeField] private LayerMask wallLayer; // make wallLayer an object
private Rigidbody2D body; // make player as an object
private Animator anim; // make animationa na object
private BoxCollider2D boxCollider; // make boxcollider an object
private float horizontalInput; // detects whether player is moving left or right
private void Awake() { // when game begins
body = GetComponent<Rigidbody2D>(); // gain player as an object
anim = GetComponent<Animator>(); //gain animation as an object
boxCollider = GetComponent<BoxCollider2D>();
}
private void Update() { // check everyframe when game is active
horizontalInput = Input.GetAxis("Horizontal"); //checks current horizontal movement
if (horizontalInput > 0.01f) { //flips character image when turning left or right
transform.localScale = Vector3.one;
} else if (horizontalInput < -0.01f) {
transform.localScale = new Vector3(-1, 1, 1);
}
//set animator parameters
anim.SetBool("Run", horizontalInput != 0);
anim.SetBool("Grounded", isGrounded());
//Jump
if (Input.GetKeyDown(KeyCode.Space)) {
jump();
}
//Dash
if (Input.GetKeyDown(KeyCode.E)) {
dash();
}
//Adjustable jump height
if (Input.GetKeyUp(KeyCode.Space) && body.velocity.y > 0) {
body.velocity = new Vector2(body.velocity.x, body.velocity.y / 2);
}
//
if (onWall()) { // allows player to stick to wall
body.gravityScale = 0;
body.velocity = Vector2.zero;
}
else {
body.gravityScale = gameGravity;
if (!isDashing) { // if player it not dashing allow them to move left and right
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
}
if (isGrounded()) { // resets both double jump & dash
jumpCounter = extraJumps; // resets counter once player touches ground
dashCounter = true;
}
}
}
private void dash() {
if (!dashCounter) return; //if player is not meant to dash
// if player is currently holding "A" down
if (Input.GetKey(KeyCode.A)) { // left
isDashing = true;
body.velocity = new Vector2(-dashDistance, 0f);
dashCounter = false;
isDashing = false;
}
// if player is currently holding "D" down
else if (Input.GetKey(KeyCode.D)) { // right
isDashing = true;
body.velocity = new Vector2(dashDistance, 0f);
dashCounter = false;
isDashing = false;
}
// if player is currently holding "W" down
else if (Input.GetKey(KeyCode.W)) { // up
isDashing = true;
body.velocity = new Vector2(0f, dashDistance);
dashCounter = false;
isDashing = false;
}
// if player is currently holding "S" down
else if (Input.GetKey(KeyCode.S)) { // down
isDashing = true;
body.velocity = new Vector2(0f, -dashDistance);
dashCounter = false;
isDashing = false;
}
}
private void jump(){ // jumping method
//SoundManager.instance.PlaySound(jumpSound);
if (jumpCounter <= 0) return; //if player is not meant to jump
if (onWall()) {
wallJump();
}
else {
if(isGrounded()) { // normal jump
body.velocity = new Vector2(body.velocity.x, jumpPower);
}
else {
if (jumpCounter > 0) { // if player can double jump let them
body.velocity = new Vector2(body.velocity.x, jumpPower);
jumpCounter--;
}
}
}
}
private void wallJump() { // wall jump
body.AddForce(new Vector2(-Mathf.Sign(transform.localScale.x) * wallJumpDistance, wallJumpHeight));
//wallJumpCooldown = 0;
}
private bool isGrounded(){ // check if player is grouned or not
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, Vector2.down, 0.1f, groundLayer);
return raycastHit.collider != null;
}
private bool onWall(){ // check if player is connected to wall or not
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, new Vector2(transform.localScale.x, 0), 0.1f, wallLayer);
return raycastHit.collider != null;
}
}