Forum begins after the advertisement:


Im trying to create the dash mechanic but its not working for some reason.

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity Im trying to create the dash mechanic but its not working for some reason.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #13257
    demilade
    Participant

    Here is my code

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerController : MonoBehaviour
    {
        [Header("Horizontal Movement Settings:")]
        [SerializeField] private float WalkSpeed = 1;
        [Space(5)]
    
        [Header("Vertical Movement Settings")]
        [SerializeField] private float JumpForce = 45;
        [SerializeField] private int JumpBufferFrames;
        [SerializeField] private float CoyoteTime;
        private int JumpBufferCounter = 0;
        private float CoyoteTimeCounter = 0;
        private int AirJumpCounter = 0;
        [SerializeField] private int MaxAirJumps = 1;
        [Space(5)]
    
        [Header("Dash Settings")]
        [SerializeField] private float DashSpeed;
        [SerializeField] private float DashTime;
        [SerializeField] private float DashCooldown;
        [Space(5)]
    
        //References
        Rigidbody2D rb;
        private float xAxis;
        public bool Grounded;
        private bool CanDash;
        private Animator anim;
        private PlayerStateList PState;
        private float Gravity;
        private bool Dashed;
    
        public static PlayerController Instance;
    
        void Start()
        {
            rb = GetComponent<Rigidbody2D>();
            anim = GetComponent<Animator>();
            PState = GetComponent<PlayerStateList>();
            Gravity = rb.gravityScale;
        }
    
        void Update()
        {
            GetInputs();
            UpdateJumpVariables();
            
            if (PState.dashing) return; //Stops running the Update function short if player is dashing
            Move();
            Jump();
            Flip();
            StartDash();
        }
    
        void GetInputs()
        {
            xAxis = Input.GetAxisRaw("Horizontal");
        }
    
        void Move()
        {
            rb.velocity = new Vector2(WalkSpeed * xAxis, rb.velocity.y);
    
            anim.SetBool("Walking", rb.velocity.x != 0 && Grounded == true);
        }
    
        void Jump()
        {
            if(Input.GetButtonUp("Jump") && rb.velocity.y > 0)
            {
                PState.jumping = false;
    
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }
            
            if (!PState.jumping)
            {
              if (JumpBufferCounter > 0 && CoyoteTimeCounter > 0)
              {
                PState.jumping = true;
    
                rb.velocity = new Vector3(rb.velocity.x, JumpForce);
              }
                else if (!Grounded == true && AirJumpCounter < MaxAirJumps && Input.GetButtonDown("Jump"))
                {
                    PState.jumping = true;
    
                    AirJumpCounter++;
    
                    rb.velocity = new Vector3(rb.velocity.x, JumpForce);
                }
            }
    
            anim.SetBool("Jumping", !Grounded == true);
        }
    
         void UpdateJumpVariables()
        {
            if (Grounded == true)
            {
                CoyoteTimeCounter = CoyoteTime;
    
                PState.jumping = false;
    
                AirJumpCounter = 0;
            }
            else
            {
                CoyoteTimeCounter -= Time.deltaTime;
            }
    
            if (Input.GetButtonDown("Jump"))
            {
                JumpBufferCounter = JumpBufferFrames;
            }
            else
            {
                JumpBufferCounter--;
            }
        }  
    
        private void OnCollisionEnter2D(Collision2D other)
        {
            if (other.gameObject.CompareTag("Floor"))
            {
                Grounded = true;
            }
        }
    
        private void OnCollisionExit2D(Collision2D other)
        {
            if (other.gameObject.CompareTag("Floor"))
            {
                Grounded = false;
            }
        }
    
        void Flip()
        {
            if (xAxis < 0)
            {
                transform.localScale = new Vector2(-1, transform.localScale.y);
            }
            else if (xAxis > 0)
            {
                transform.localScale = new Vector2(1, transform.localScale.y);
            }
        }
    
        private void Awake()
        {
            if(Instance != null && Instance != this)
            {
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
        }
    
        IEnumerator Dash() //the dash action the player performs
        {
            CanDash = false;
            PState.dashing = true;
            anim.SetTrigger("Dashing");
            rb.gravityScale = 0;
            rb.velocity = new Vector2(transform.localScale.x * DashSpeed, 0);
            yield return new WaitForSeconds(DashTime);
            rb.gravityScale = Gravity;
            PState.dashing = false;
            yield return new WaitForSeconds(DashCooldown);
            CanDash = true;
        }
        
        void StartDash()
        {
            if(Input.GetButtonDown("Dash") && CanDash && !Dashed)
            {
                StartCoroutine(Dash());
                Dashed = true;
            }
    
            if (Grounded == true)
            {
                Dashed = false;
            }
        }
    }

    I used a different method for the groundcheck in case you’re wondering.

    #13261
    Terence
    Keymaster

    Can you add the highlighted lines below and show me what the Console displays when you dash?

    IEnumerator Dash() //the dash action the player performs
    {
        CanDash = false;
        PState.dashing = true;
        anim.SetTrigger("Dashing");
        rb.gravityScale = 0;
        rb.velocity = new Vector2(transform.localScale.x * DashSpeed, 0);
        print("Dash coroutine");
        yield return new WaitForSeconds(DashTime);
        rb.gravityScale = Gravity;
        PState.dashing = false;
        print("Dash end");
        yield return new WaitForSeconds(DashCooldown);
        CanDash = true;
        print("Dash cooled down");
    }
        
    void StartDash()
    {
        if(Input.GetButtonDown("Dash") && CanDash && !Dashed)
        {
            print("Dash starting");
            StartCoroutine(Dash());
            Dashed = true;
        }
    
        if (Grounded == true)
        {
            Dashed = false;
        }
    }
    #13267
    Daikuna
    Participant

    Change your private bool canDash; to private bool canDash = true; That should fix it for you. Had the same issue with mine recently.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: