#Whenever I dash though spikes, the respawn is glitchy.

1 messages · Page 1 of 1 (latest)

livid burrow
#

(Player respawn)


public class PlayerRespawn : MonoBehaviour
{
    private Transform currentCheckpoint; //Storeing our last checkpoint here
    private Health playerHealth;

    private void Awake()
    {
        playerHealth = GetComponent<Health>();
    }

    public void Respawn()
    {
        transform.position = currentCheckpoint.position; //move player to checkpoint position
        playerHealth.Respawn(); // Restore the player health

        // Move camera to checkpoint room
        Camera.main.GetComponent<CameraController>().MoveToNewRoom(currentCheckpoint.parent);
    }

    // Activate checkpoint
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.transform.tag == "Checkpoint")
        {
            currentCheckpoint = collision.transform;
            collision.GetComponent<Collider2D>().enabled = false;
        }
    }
}```
#

.
.
.
.
(Dash Script)


public class PlayerDash : MonoBehaviour
{
    [Header("Dash Settings")]
    public float dashDistance = 2f; 
    public float dashSpeed = 2f; 
    public int maxDashes = 3;
    public float rayLength = 8f;
    public LayerMask dashLayerMask;     // Assign Ground + Button layers in Inspector

    private int dashCount = 0;
    private Rigidbody2D rb;
    private bool isDashing = false;
    private bool wasGrounded = false;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        bool groundedNow = IsGrounded();

        // Reset dash count when landing
        if (groundedNow && !wasGrounded)
        {
            dashCount = 0;
        }

        wasGrounded = groundedNow;

        // Dash input
        if (Input.GetKeyDown(KeyCode.K) && dashCount < maxDashes)
        {
            Vector2 dashDir = Vector2.zero;

            if (Input.GetKey(KeyCode.W)) dashDir = Vector2.up;
            else if (Input.GetKey(KeyCode.S)) dashDir = Vector2.down;
            else if (Input.GetAxisRaw("Horizontal") > 0) dashDir = Vector2.right;
            else if (Input.GetAxisRaw("Horizontal") < 0) dashDir = Vector2.left;

            if (dashDir != Vector2.zero)
                AttemptDash(dashDir);
        }
    }

    private void AttemptDash(Vector2 dir)
{
    // Check raycast for interactions
    RaycastHit2D hit = Physics2D.Raycast(transform.position, dir, rayLength, dashLayerMask);

    if (hit.collider != null)
    {
        if (hit.collider.CompareTag("Button"))
        {
            // Cancel dash, but activate button
            hit.collider.GetComponent<ButtonTrigger>()?.PressButton();
            LoseFallingMomentum(); // <- clear falling momentum
            return;
        }
    }

    // Always dash a fixed distance
    Vector2 targetPos = rb.position + dir.normalized * dashDistance;
    StartCoroutine(DashRoutine(targetPos));
}```
#
{
    isDashing = true;
    dashCount++;

    Vector2 startPos = rb.position;
    float distance = Vector2.Distance(startPos, targetPos);
    float duration = distance / dashSpeed; // time = dist / speed
    float t = 0f;

    while (t < 1f)
    {
        t += Time.deltaTime / duration;

        // Step movement
        Vector2 newPos = Vector2.Lerp(startPos, targetPos, t);

        // Check for enemy in between
        RaycastHit2D enemyHit = Physics2D.Linecast(rb.position, newPos, LayerMask.GetMask("Enemy"));
        if (enemyHit.collider != null && enemyHit.collider.CompareTag("Enemy"))
        {
            // Stop dash at collision point
            rb.MovePosition(enemyHit.point);
            break;
        }

        rb.MovePosition(newPos);
        yield return null;
    }

    isDashing = false;

    // Lose falling momentum when dash ends
    LoseFallingMomentum();
}


private void LoseFallingMomentum()
{
    if (rb.linearVelocity.y < 0)
    {
        rb.linearVelocity = new Vector2(rb.linearVelocity.x, 0f);
    }
}


    private bool IsGrounded()
    {
        return Physics2D.Raycast(transform.position, Vector2.down, 0.8f, LayerMask.GetMask("Ground"));
    }
}```
proven cedar
# livid burrow

video must be mp4 to embed in discord.
post !code using one of the links below 👇 instead of pasting as message

deft sparrowBOT
deft sparrowBOT
vestal stump
#

The DashRoutine does not seem to end by any means when dying , so the dash continues after death and you get teleported back to where the dash was happening. Im not sure the code is very hard to read without it being properly formatted