- Home /
The question is answered, right answer was accepted
Can I increase gravity / fallspeed of instantiated prefabs over time?
Hello! I am working on a 2D color matching / catch game and I am trying to increase the gravity of my instantiated objects so the longer the game goes on, the faster they fall. Here is the code I am using to instantiate my prfabs:
using UnityEngine;
using System.Collections;
public class SpawnBox : MonoBehaviour
{
public GameObject[] boxList;
private bool gameOver;
public GameObject gameOverText;
public GameObject restartButton;
public GameObject menuButton;
void Start()
{
gameOver = false;
StartCoroutine(SpawnNewBox());
}
IEnumerator SpawnNewBox()
{
yield return new WaitForSeconds (1.75f);
while (!gameOver)
{
int i = Random.Range (0, boxList.Length);
Instantiate (boxList [i], transform.position, Quaternion.identity);
yield return new WaitForSeconds (Random.Range (2.0f, 3.1f));
}
GameObject.FindGameObjectWithTag("MainCamera").GetComponent<AudioSource>().Stop();
GameObject.FindGameObjectWithTag("Destroyer").GetComponent<AudioSource>().Play();
gameOverText.SetActive(true);
yield return new WaitForSeconds (1.5f);
restartButton.SetActive(true);
menuButton.SetActive(true);
}
public void GameOver()
{
gameOver = true;
}
}
I have tried using InvokeRepeating and AddForce in the start function and in an update function but with no luck. Any assistance / guidance is greatly appreciated!!
Add a script to the instantiated GameObject which increased the velocity over time. You could also keep references to all instantiated GOs in your existing script and loop through them every frame (or every few seconds, or via Lerp) in a seperate Coroutine to increase their velocity over time.
So I put this script onto the prefabs ( there are six of them) that I'm instantiating but don't see an increase in speed at all:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpeedUp : $$anonymous$$onoBehaviour {
public float speed;
public float IncreaseAmount;
void Start ()
{
InvokeRepeating ("IncreaseSpeed", 3.0f, 3.0f);
}
void IncreaseSpeed ()
{
speed += IncreaseAmount;
}
I added values to speed and IncreaseAmount in the editor, and I don't receive any errors...so I feel like there is a very simple thing I am overlooking / missing..
"speed" is just a variable you declared, it doesn't do anything. You have to assign it's value to the rigidbody's gravity or downward speed or whatever in order to have any effect.
Answer by BattiestMist · Jun 25, 2017 at 04:53 PM
//Called to change gravity based on the amount specified
void ChangeGravity(float amount) {
//Note: The amount is made negative so that gravity will take objects downwards
Physics.gravity += new Vector3(0, -amount, 0);
}
Call this with InvokeRepeating on any object in the scene.
Got it working! But now The invoke continues on through different scenes, and even after my restart button is pressed to restart the game...The only way to reset the invoke is to close out of the application and start again. I've tried using CanceInvoke() in a couple different places, most recently here: void Start () { InvokeRepeating ("changeGravity", 5.0f, 5.0f); } void changeGravity () { Physics2D.gravity += new Vector2 (0, -2); } void Update() { if (Input.GetButtonDown ("Restart Button")) { CancelInvoke (); } } }
Again, no errors :( I've also tried putting CancelInvoke() as a part of my GameOver function and calling it from a different script but still no luck...What the heck am I missing?
You may be cancelling the adding of force to the rigid body but it still retains its velocity.
Rigidbody.Velocity = Vector3.zero;
$$anonymous$$ight work.
I should note that this is only if you're sure your invoke is getting canceled.
Couldn't get that line to work. I'm not exactly sure how, but the invoke isn't being cancelled. I can reload the scene all day via the restart & menu buttons but the increase in speed continues. The only way to "reset it" per say is to exit play mode in the editor / close the app on the phone.