#Dash duration only works, when selecting the object in the inspector.

1 messages · Page 1 of 1 (latest)

strong cosmos
#

The playable character in the scene has a dash function that, once activated, has a specific duration. When I select the object in the inspector it works fine, but when I select another one or play it in a build, the dash duration is always seems to be faster. Does anyone know what the cause of this could be?

{
    if (stamina >= 0 + dashCost)
    {
        //Sound Effekt
        DashSound.PlayOneShot(DashSound.clip);

        rb.linearVelocity = Vector3.zero;
        gravityScale.gravityEnabled = false;

        Vector3 dashDirection = transform.forward;

        if (Input.GetKey(KeyCode.A))
        {
            dashDirection = transform.right * -1;
        }
        if (Input.GetKey(KeyCode.D))
        {
            dashDirection = transform.right;
        }
        if (Input.GetKey(KeyCode.S))
        {
            dashDirection = transform.forward * -1;
        }

        dashing = true;
        rb.AddForce(dashDirection * dashForce, ForceMode.Impulse);

        stamina -= dashCost;
        staminaUsed = true;

        StartCoroutine(DashControl());
    }
}

private IEnumerator DashControl()
{
    dashDuration = 0;
    dashMaxSpeed = dashMaxSpeedSave;

    while (dashDuration < dashDurationSave)
    {
        dashMaxSpeed = Mathf.Lerp(dashMaxSpeedSave, maxSpeed, dashDuration / dashDurationSave);
        dashDuration += Time.fixedDeltaTime;
        yield return null;
    }

    gravityScale.gravityEnabled = true;
    dashing = false;
}```
shadow cairn
#

You're doing a while loop which yields for one frame at a time but it increases the timer by a fixed value each frame

#

(Time.fixedDeltaTime)

wintry root
shadow cairn
#

Because drawing the inspector changes the framerate

#

Simply because it's a thing that makes the computer work harder

shadow cairn
#

You need to switch to regular Time.deltsTime OR switch to yield return WaitForFixedUpdate

#

One or the other

strong cosmos
#

I forgot to put it into the code but the function (DashControl) is on Fixed Update

{
    if (movementEnabled == true)
    {
        MovePlayer();
        Hover();
    }

    GroundedCheck();

    if (!grounded)
    {
        WallCheck();
    }

    SpeedControl();
    DashControl();
}```
shadow cairn
#

This call does nothing

wintry root
#

ye just put yield return WaitForFixedUpdate

shadow cairn
#

It's the StartCoroutine(DashControl()) inside Dash() that runs it

#

Calling a coroutine without StartCoroutine does absolutely nothing

strong cosmos
#

alr I'll try with normal update in the function

#

I mean delta time

#

Yeah that fixed it

#

thanks 🙂

wintry root
strong cosmos
#

I dont really know why that was. I'm not the only one working on the game and this part was not written my me.

wintry root
#

oh ok alright

gaunt briar
#

But yeah it is the incorrect way to do it

shadow cairn
gaunt briar
#

Yep my bad - not sure where that expectation came from. Just tested and it works as you said