codes combined (dash and jump)

This commit is contained in:
cemt1363
2026-06-06 01:09:39 +02:00
parent 5c49f2b34b
commit 342489922e
2 changed files with 64 additions and 13 deletions
+63 -12
View File
@@ -1,19 +1,24 @@
using UnityEngine;
using System.Collections;
public class DropletMovement : MonoBehaviour
{
private Rigidbody2D rb;
public float moveSpeed = 5f;
public float jumpingPower = 5f;
private Vector2 movement;
private Animator playerAnim; //for animation
private float xInput;
//helper variables
public float jumpingPower = 5f;
public float moveSpeed = 5f;
private Vector2 movement;
private bool isGrounded = true;
[SerializeField] private float dashSpeed;
[SerializeField] private float dashDuration;
private float dashTime;
private bool canDash = true;
private bool isDashing;
public float dashingPower = 24f;
public float dashingTime = 0.2f;
public float dashingCooldown = 1f;
[SerializeField] private TrailRenderer tr;
void Start()
{
@@ -22,18 +27,64 @@ public class DropletMovement : MonoBehaviour
void Update()
{
if (isDashing)
{
return;
}
movement.x = Input.GetAxisRaw("Horizontal");
if (isGrounded && Input.GetKeyDown(KeyCode.Space))
if (isGrounded &&
(Input.GetKeyDown(KeyCode.Space) ||
Input.GetKeyDown(KeyCode.UpArrow) ||
Input.GetKeyDown(KeyCode.W)))
{
rb.linearVelocity = new Vector2(rb.linearVelocity.x, jumpingPower);
}
if (Input.GetKeyDown(KeyCode.LeftShift) && canDash)
{
StartCoroutine(Dash());
}
}
private void FixedUpdate()
{
if (isDashing)
{
return;
}
rb.linearVelocity = new Vector2(movement.x * moveSpeed, rb.linearVelocity.y);
}
private IEnumerator Dash()
{
canDash = false;
isDashing = true;
float originalGravity = rb.gravityScale;
rb.gravityScale = 0f;
rb.linearVelocity = new Vector2(transform.localScale.x * dashingPower, 0f);
if (tr != null)
{
tr.emitting = true;
}
yield return new WaitForSeconds(dashingTime);
if (tr != null)
{
tr.emitting = false;
}
rb.gravityScale = originalGravity;
isDashing = false;
yield return new WaitForSeconds(dashingCooldown);
canDash = true;
}
}