0
\$\begingroup\$

I want my gameobjects to fall faster by time as the player collides with them. What I mean is, when the game starts, the gameobjects fall off after 1 second the player collided with them, but after 60 seconds, gameobjects only fall after 0.3 seconds after the player has been collided with them. The idea is to give the player less time to jump from a platform (gameobject) to another platform as the time goes, so it will be more challenging.

Here is what I've made so far:

private Rigidbody rb;
public float currentFallDelay = 1f;
float minFallDelay = 0.3f;
public float accelerationTime = 10;
private float maxFallDelay;
private float time;

void Start()
{
    rb = GetComponent<Rigidbody>();
    minFallDelay = currentFallDelay;
    time = 0;
}

public IEnumerator FallDelay()
{
    rb.isKinematic = true;
    yield return new WaitForSeconds(1f);
    while (true)
    {
        currentFallDelay = Mathf.SmoothStep(minFallDelay, maxFallDelay, time / accelerationTime);
        transform.position += Vector3.down * currentFallDelay * Time.deltaTime;
        time += Time.deltaTime;
        yield return null;
    }
}

void OnCollisionEnter(Collision col)
{

    if (rb.isKinematic && col.collider.CompareTag("Player"))
    {
        StartCoroutine(FallDelay());
    }

}

I tried to use yield return new WaitForSeconds(1f) inside FallDelay() but of course it didn't change the fall delay after time. Can anyone help me how to fix that?

Helps are appreciated!

\$\endgroup\$
2
  • \$\begingroup\$ In future, please edit your existing question instead of deleting it and posting a duplicate. This kind of action can look like an attempt to evade moderation, which can cause some automated anti-spam scripts to apply restrictions to your account. \$\endgroup\$ Commented Jan 6, 2022 at 2:01
  • \$\begingroup\$ Sorry, I was afraid that if I almost 100% edit the question, then it won't be accepted. Now I know, thank you! \$\endgroup\$ Commented Jan 6, 2022 at 2:29

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.