I’ve been trying to implement a dash move in my 2d platform game, but it just doesn’t work well.
My script:
[SIZE=1]using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Controller : MonoBehaviour
{
// Basics
public float speed;
public float jumpforce;
private float moveInput;
private bool isGrounded;
public Transform groundCheck;
public float gCheckRadiusX;
public float gCheckRadiusY;
public LayerMask whatIsGround;
private Rigidbody2D rb;
//Double Jump
private int extraJumps;
public int extraJumpsValue;
//Jump hold
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
public float jumpLimit;
private float jumpLimitCounter;
// Wall Jump
bool isTouchingFront;
public Transform frontCheck;
public float fCheckRadiusX;
public float fCheckRadiusY;
bool wallSliding;
public float wallSlidingSpeed;
bool wallJumping;
public float xWallForce;
public float yWallForce;
public float wallJumpTime;
private float wallJumpTimeCounter;
// Dash
public float dashSpeed;
public float startDashTime;
private float dashTime;
private int direction;
void Start(){
rb = GetComponent<Rigidbody2D>();
extraJumps = extraJumpsValue;
}
void FixedUpdate(){
isGrounded = Physics2D.OverlapBox(groundCheck.position, new Vector2(gCheckRadiusX, gCheckRadiusY), 0, whatIsGround);
if(wallJumping != true){
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
}
void Update(){
if(isGrounded == true)
extraJumps = extraJumpsValue;
if(moveInput > 0){
transform.eulerAngles = new Vector3(0, 0, 0);
} else if(moveInput < 0){
transform.eulerAngles = new Vector3(0, 180, 0);
}
//Jump
if(Input.GetKeyDown(KeyCode.X)&& extraJumps > 0){
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpforce;
extraJumps--;
}
if(Input.GetKey(KeyCode.X) && isJumping == true){
if(jumpTimeCounter > 0){
rb.velocity = (Vector2.up * jumpforce) * 3/2;
jumpTimeCounter -= Time.deltaTime;
} else {
isJumping = false;
}
}
if(Input.GetKeyUp(KeyCode.X)){
isJumping = false;
}
// Wall Jump
isTouchingFront = Physics2D.OverlapBox(frontCheck.position, new Vector2(fCheckRadiusX, fCheckRadiusY), 0, whatIsGround);
if (isTouchingFront == true && isGrounded == false && moveInput != 0)
{
wallSliding = true;
} else {
wallSliding = false;
}
if (wallSliding)
{
rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
if (Input.GetKeyDown(KeyCode.X) && wallSliding == true)
{
wallJumping = true;
wallJumpTimeCounter = wallJumpTime;
rb.velocity = new Vector2(xWallForce * -moveInput, yWallForce);
}
if(Input.GetKey(KeyCode.X) && wallJumping == true){
if(wallJumpTimeCounter > 0){
rb.velocity = new Vector2(xWallForce * -moveInput, yWallForce) * 3/2;
wallJumpTimeCounter -= Time.deltaTime;
} else {
wallJumping = false;
}
}
if(Input.GetKeyUp(KeyCode.X)){
wallJumping = false;
}
// Dash
if (direction == 0)
{
if (Input.GetKeyDown(KeyCode.Z))
{
if (moveInput < 0)
{
direction = 1;
}
else if (moveInput > 0)
{
direction = 2;
}
}
}
else
{
if (dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
rb.velocity = Vector2.zero;
}
else
{
dashTime -= Time.deltaTime;
if (direction == 1)
{
rb.velocity = Vector2.left * dashSpeed;
}
else if (direction == 2)
{
rb.velocity = Vector2.right * dashSpeed;
}
}
}
}
void OnDrawGizmosSelected(){
Gizmos.color = Color.green;
Gizmos.DrawWireCube(groundCheck.position, new Vector3(gCheckRadiusX, gCheckRadiusY, 1));
Gizmos.DrawWireCube(frontCheck.position, new Vector3(fCheckRadiusX, fCheckRadiusY, 1));
}
}[/SIZE]
Even if the public float of “startDashTime” works perfectly, the player’s speed doesn’t change, even if I modify the dash speed.
Also, if I make the dash Input in the air, the player starts falling slower than normal, but again, his speed still remains the same.