dropwdown

This commit is contained in:
orsolya-st
2026-06-06 02:10:19 +02:00
parent 6dd331b550
commit e40669b234
+46 -4
View File
@@ -30,16 +30,15 @@ public class DropletMovement : MonoBehaviour
void Update()
{
//dashing
if (isDashing)
{
return;
}
movement.x = Input.GetAxisRaw("Horizontal");
if (movement.x < 0) facingDirection = -1;
if (movement.x > 0) facingDirection = 1;
if (isGrounded &&
(Input.GetKeyDown(KeyCode.Space) ||
Input.GetKeyDown(KeyCode.UpArrow) ||
@@ -47,11 +46,18 @@ public class DropletMovement : MonoBehaviour
{
rb.linearVelocity = new Vector2(rb.linearVelocity.x, jumpingPower);
}
if (Input.GetKeyDown(KeyCode.LeftShift) && canDash)
{
StartCoroutine(Dash());
}
//dropdown
if (!isGrounded){return;}
if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
{
DropDown();
}
}
private void FixedUpdate()
@@ -103,4 +109,40 @@ public class DropletMovement : MonoBehaviour
{
isGrounded = false;
}
private void DropDown()
{
//when "s" or "down key" is pressed turn of/disable collider;
//move object slowly down (1 seconds, move down the height of the collision box)
//after that enable collider again
if (isGrounded && (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)))
{
StartCoroutine(DropRoutine());
}
}
private IEnumerator DropRoutine()
{
Collider2D playerCollider = GetComponent<Collider2D>();
playerCollider.isTrigger = true;
float duration = 1f;
float elapsed = 0f; //measure time
//move down the height of the collider
float dropDistance = playerCollider.bounds.size.y;
float speed = dropDistance/duration;
while (elapsed < duration)
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
elapsed += Time.deltaTime; //update time
yield return null;
}
//enable collider --> not a trigger anymore
playerCollider.isTrigger = false;
}
}