- Home /
 
Problem is not reproducible or outdated
2d platformer script problems
Hi i am new to programming and this probably is not the best script for what im trying to achieve but i have it kind of functioning. I am trying to make a 2d platformer game.
In this script i have to use fixed update because thats the only way i can get it to work, but some times when i press the dash key its not fully responsive is there a reason why?
Ideally i would like to have my player dash once, left, right and diagonally and have a cool down time so cant keep dashing until my player is grounded again but due to my skills that limits me alot. If anyone could help me with a better way that would be hugely appreciated but i guess people are busy.
Or even if someone can set me on the right track to acheive what im after that would be great! thanks heres the script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
 public class DashAbility : MonoBehaviour
 {
 
     public DashState dashState;
     public float dashTimer;
     public float maxDash = 20f;
 
 
     public float dashSpeed;
     public float returnMovementSpeed;
 
     public bool dashingRight;
     public bool dashingLeft;
 
 
     Rigidbody2D rb;
     PlayerController playerControllerScript;
 
 
 
      void Start()
     {
         rb = GetComponent<Rigidbody2D>();
 
         playerControllerScript = GetComponent<PlayerController>();
 
         returnMovementSpeed = playerControllerScript.speed;
         
     }
 
     private void FixedUpdate()
     {
         switch (dashState)
         {
             case DashState.Ready:
                 var isDashKeyDown = Input.GetKeyDown(KeyCode.LeftShift);
                 if (isDashKeyDown && playerControllerScript.facingRight == true)
 
                 {
                     dashState = DashState.Dashing;
                     dashingRight = true;
                     dashingLeft = false;
                 }
 
                 if (isDashKeyDown && playerControllerScript.facingRight == false)
 
                 {
                     dashState = DashState.Dashing;
                     dashingRight = false;
                     dashingLeft = true;
                 }
                 break;
             case DashState.Dashing:
                 dashTimer += Time.deltaTime;
                 if (dashTimer >= maxDash && dashingRight == true)
                 {
                     dashTimer = maxDash;
                     rb.velocity = transform.right * dashSpeed;
                     playerControllerScript.speed = 0f;
                     dashingLeft = false;
                     dashState = DashState.Cooldown;
 
                 }
                 if (dashTimer >= maxDash && dashingLeft == true)
                 {
                     dashTimer = maxDash;
                     rb.velocity = -transform.right * dashSpeed;
                     playerControllerScript.speed = 0f;
                     rb.gravityScale = 0;
                     dashingRight = false;
                     dashState = DashState.Cooldown;
                 }
 
 
 
                 break;
             case DashState.Cooldown:
                 dashTimer -= Time.deltaTime;
                 if (dashTimer <= 0)
                 {
                     dashTimer = 0;
                     dashingRight = false;
                     dashingLeft = false;
                     rb.gravityScale = 4;
                     playerControllerScript.speed = returnMovementSpeed;
 
                     dashState = DashState.Ready;
                 }
                 break;
         }
     }
 }
 
 public enum DashState
 {
     Ready,
     Dashing,
     Cooldown
 }
 
              Follow this Question
Related Questions
Having some stuck issues on the 2D infinite runner 0 Answers
Unity crashes 0 Answers
Way to achieve 3 lane mechanism in a 2D game 0 Answers
HOLD JUMP BUTTON TO JUMP HIGHER 2 Answers