- Home /
How to change fall delay of a gameobject by time?
I want my gameobjects to fall faster as the time goes. Gameobject falling is getting called when the player collides with if.
Here's my code:
private Rigidbody rb;
public float currentFallDelay = 1f;
float minFallDelay = 0.3f;
public float accelerationTime = 60;
private float maxFallDelay;
private float time;
void Start()
{
rb = GetComponent<Rigidbody>();
minFallDelay = currentFallDelay;
time = 0;
}
public void FallDelay()
{
rb.isKinematic = true;
currentFallDelay = Mathf.SmoothStep(minFallDelay, maxFallDelay, time / accelerationTime);
transform.position += Vector3.down * currentFallDelay * Time.deltaTime;
time += Time.deltaTime;
}
void OnCollisionEnter(Collision col)
{
if(rb.isKinematic && col.collider.CompareTag("Player"))
{
FallDelay();
}
}
What am I doing wrong? All helps are appreciated!
Answer by daotanp · Jan 02 at 07:09 AM
Maybe something like this:
Transform.position += Vector3.down * currentFallDelay * time;
As the value of time increase, the object should fall faster.
As you can see in the 19th line of my coding, I did the same exact thing but it didn't work.
I mean you should change Time.deltaTime to time (in the 19th line you multiply Vector3.down with currentFallDelay and Time.deltaTime) because time increase each frame (20th line) and Time.deltaTime is just the time between 2 frame.
Answer by Aluminum18 · Jan 02 at 12:31 PM
currentFallDelay = Mathf.SmoothStep(minFallDelay, maxFallDelay, time / accelerationTime);
transform.position += Vector3.down * currentFallDelay * Time.deltaTime;
time += Time.deltaTime;
The problem is above code block is just executed once, so your object's position only changes in one frame. The logic that changes object's position for making it moving need to be executed every frame. In your case, Coroutine could help.
public IEnumerator FallDelay()
{
rb.isKinematic = true;
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());
}
}
Thank you for your reply! I also tried using Coroutine, but the problem with that is, that as soon as the player collides with the gameobject, the player falls with it.
This game is jump based, you have to move forward on platforms (gameobjects) as fast as you can.
What you need is that the player should have a short period to jump to next platform before the current platform starts to fall, right? If so, you can add a small delay to fall logic.
public IEnumerator FallDelay()
{
rb.isKinematic = true;
yield return new WaitForSeconds(0.2f) // delay 0.2 second, adjust it as you need
while (true)
{
currentFallDelay = Mathf.SmoothStep($$anonymous$$FallDelay, maxFallDelay, time / accelerationTime);
transform.position += Vector3.down * currentFallDelay * Time.deltaTime;
time += Time.deltaTime;
yield return null;
}
}
Well, sorry if my explanation is unclear. So, what I want is: when the player collides with platform at the beginning of the game, the platform is starting fall down after 1 second. The fall delay decreases (up to 0.3f) until the time has been reached 60 seconds.