- Home /
How to delay gameobject from falling by time?
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!
Answer by henkehedstrom · Jan 07 at 11:19 AM
Instead of using a 1 inside of WaitForSeconds you could use a variable that decreases over time in your update loop. Do something like currentFallDelay -= 0.1 * Time.deltaTime; That would minimize the delay by 0.1 seconds every second. You could of course use a lerp function or similar for this and it is wise to ensure that your delay wont get below your minimum but I think you know how to do that already! (Edited this paragraph because I misunderstood some code)
However, wont the object fall slower aswell? If the currentFallDelay is 0.3f it will take 0.3 seconds until it starts falling but you then multiply the transform.position with the falldelay. You will change the position less the lower the fallDelay is.
I might just be a bit confused and not understand the math but I don't think you want to multiply with the falldelay there.
If you don't want to do the falling yourself you could probably just enable/disable useGravity on the rigidbody instead.
Yes, I also experienced that the object keeps falling slower because I am affecting the transform but I haven't really pay much attention to this because that's not the biggest issue here.
Did it work by changing the 1 to currentFallDelay?
Yes, I did but the fall delay did not decreased by time.
Your answer
Follow this Question
Related Questions
How to change fall delay of a gameobject by time? 2 Answers
how to make the sprite and the background color in a tile disappear? 0 Answers
Unity2D - how to move object to the touched position 1 Answer
Image material on an object plays in Unity Editor but in Android it doesn’t 0 Answers
How do I get into the data returned from a UnityWebRequest ? 2 Answers