Forum begins after the advertisement:
[Part 2] my dash dont work, when i use dash while jumping it speed up the jump
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 2] my dash dont work, when i use dash while jumping it speed up the jump
- This topic has 3 replies, 2 voices, and was last updated 10 months ago by Terence.
Viewing 4 posts - 1 through 4 (of 4 total)
-
AuthorPosts
-
February 19, 2024 at 5:53 am #13352::
For some reason it only plays the dash animation, but it stand still, I don’t know why
View post on imgur.com
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { [Header("Horizontal Movement Settings")] private Rigidbody2D rb; [SerializeField] private float walkspeed = 1; private float xAxis; [Header("Jumping Settings")] [SerializeField] private float jumpForce = 45; private int jumpBufferCounter = 0; [SerializeField] private int jumpBufferFrames; private float coyoteTimeCounter = 0; [SerializeField] private float coyoteTime; private int airJumpCounter = 0; [SerializeField] private int maxAirJumps; [Header("Ground Check Settings")] [SerializeField] private Transform groundCheckPoint; [SerializeField] private float groundCheckY = 0.2f; [SerializeField] private float groundCheckX = 0.5f; [SerializeField] private LayerMask whatIsGround; [SerializeField] private float dashSpeed; [SerializeField] private float dashTime; [SerializeField] private float dashCooldown; Animator anim; PlayerStateList pState; private bool canDash = true; private bool dashed; private float gravity; public static PlayerController Instance; private void Awake() { if(Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } } // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody2D>(); pState = GetComponent<PlayerStateList>(); anim = GetComponent<Animator>(); gravity = rb.gravityScale; } // Update is called once per frame void Update() { GetInputs(); UpdateJumpVariables(); if(pState.dashing) return; Flip(); Move(); Jump(); StartDash(); } void GetInputs() { xAxis = Input.GetAxisRaw("Horizontal"); } 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 Move() { rb.velocity = new Vector2(walkspeed * xAxis, rb.velocity.y); anim.SetBool("Walking", rb.velocity.x != 0 && Grounded()); } void StartDash() { if(Input.GetButtonDown("Dash") && canDash && !dashed) { StartCoroutine(Dash()); dashed = true; } if (Grounded()) { dashed = false; } } IEnumerator Dash() { canDash = false; pState.dashing = true; anim.SetTrigger("Dashing"); rb.gravityScale = 0; yield return new WaitForSeconds(dashTime); rb.gravityScale = gravity; pState.dashing = false; yield return new WaitForSeconds(dashCooldown); canDash = true; } public bool Grounded() { if (Physics2D.Raycast(groundCheckPoint.position, Vector2.down, groundCheckY, whatIsGround) || Physics2D.Raycast(groundCheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround) || Physics2D.Raycast(groundCheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround)) { return true; } else { return false; } } void Jump() { if (Input.GetButtonUp("Jump") && rb.velocity.y > 0) { rb.velocity = new Vector2(rb.velocity.x, 0); pState.jumping = false; } if (!pState.jumping) { if (jumpBufferCounter > 0 && coyoteTimeCounter > 0) { rb.velocity = new Vector3(rb.velocity.x, jumpForce); pState.jumping = true; } else if(!Grounded() && airJumpCounter < maxAirJumps && Input.GetButtonDown("Jump")) { pState.jumping = true; airJumpCounter++; rb.velocity = new Vector3(rb.velocity.x, jumpForce); } } if (Input.GetButtonDown("Jump") && Grounded()) { rb.velocity = new Vector3(rb.velocity.x, jumpForce); pState.jumping = true; } anim.SetBool("Jumping", !Grounded()); } void UpdateJumpVariables() { if (Grounded()) { pState.jumping = false; coyoteTimeCounter = coyoteTime; airJumpCounter = 0; } else { coyoteTimeCounter -= Time.deltaTime; } if(Input.GetButtonDown("Jump")) { jumpBufferCounter = jumpBufferFrames; } else { jumpBufferCounter--; } } }
February 19, 2024 at 3:53 pm #13354::Hi Slenderdog 20, can you add the following lines to your dash code? After that, do a dash in the game and show me what is printed in the Console.
void StartDash() { if(Input.GetButtonDown("Dash") && canDash && !dashed) { print("Start dash"); StartCoroutine(Dash()); dashed = true; } if (Grounded()) { dashed = false; } } IEnumerator Dash() { canDash = false; pState.dashing = true; anim.SetTrigger("Dashing"); print("Dashing started"); rb.gravityScale = 0; yield return new WaitForSeconds(dashTime); rb.gravityScale = gravity; pState.dashing = false; print("Dashing ended"); yield return new WaitForSeconds(dashCooldown); canDash = true; print("Dashing cooled down"); }
February 20, 2024 at 6:41 am #13359February 21, 2024 at 12:38 am #13364::I found the issue. Your dash coroutine is missing this line:
IEnumerator Dash() { canDash = false; pState.dashing = true; anim.SetTrigger("Dashing"); print("Dashing started"); rb.gravityScale = 0; rb.velocity = new Vector2(transform.localScale.x * dashSpeed, 0); yield return new WaitForSeconds(dashTime); rb.gravityScale = gravity; pState.dashing = false; print("Dashing ended"); yield return new WaitForSeconds(dashCooldown); canDash = true; print("Dashing cooled down"); }
-
AuthorPosts
Viewing 4 posts - 1 through 4 (of 4 total)
- You must be logged in to reply to this topic.
Advertisement below: