Question by
cordial_studios · Sep 29, 2020 at 10:59 AM ·
platformer
how do i make the player dash upwards?
I'm making a game where the player cant jump (the jump is under Input.GetKeyDown(KeyCode.Space))) and i need to replace jumping with up dashing
//Ground / Inputcheck
private float moveInput;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
//Jump
private float jumpTimeCounter;
public float jumpForce;
public float jumpTime;
private bool isJumping;
private bool facingRight = true;
//Dash
public float dashSpeed = 30f;
public float startDashTime = 0.1f;
private float dashTime;
private int direction;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
//Dash
dashTime = startDashTime;
}
private void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if (Input.GetKey(KeyCode.Space) && isJumping == true)
{
if (jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{
Flip();
}
void Flip()
{
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
//dashing
if (direction == 0)
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
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;
}
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613012728im_/https://answers.unity.com/themes/thub/images/avi.jpg)