Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 29, 2017 at 11:11 PM by DKitch9 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DKitch9 · Jun 24, 2017 at 05:34 PM · instantiatecoroutinegravity2d-physicsinvokerepeating

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!!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Jun 25, 2017 at 01:31 AM 0
Share

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.

avatar image DKitch9 Cherno · Jun 25, 2017 at 03:55 PM 0
Share

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..

avatar image Cherno DKitch9 · Jun 26, 2017 at 02:26 AM 0
Share

"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.

1 Reply

  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DKitch9 · Jun 25, 2017 at 07:36 PM 0
Share

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?

avatar image EpsilonQoppa DKitch9 · Jun 26, 2017 at 01:13 AM 0
Share

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.

avatar image DKitch9 EpsilonQoppa · Jun 26, 2017 at 02:11 AM 0
Share

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.

Follow this Question

Answers Answers and Comments

87 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Horizontal attraction? 0 Answers

How to Instantiate prefabs with function Update? 1 Answer

gravity change via controls(help) 2d,Anyone know how to fix this gravity thing 1 Answer

Spawned Enemies: How to Destroy child objects inside their own attached scripts without Destroying the parent object for InvokeRepeating?? 1 Answer

How to Spawn multiple game object one by one in random order 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges