Search code examples
c#positionunity-game-engine

How to Get Constant Force To Increase Over Time? (Unity 5)


My game is an over-the-shoudler endless runner style game. I have the obstacles coming down the screen using constant force. My question is, how do I make this Constant Force Script make the obstacle go gradually faster as the game progresses and the player goes longer into the game? If you try out this script yourself (just attach it to a cube or a sphere), you'll notice how it starts off slow at first then literally 2 seconds later, it goes into Rocket Speed lol. ( I am working on an Object Pooler Script also, since I do not want to waste CPU with Instantiate and Destroy) Thank you to all who attempt and help me solve this! :)

Here is the Script I made:

using UnityEngine;
 using System.Collections;

 [RequireComponent(typeof(ConstantForce))]

 public class AddConstantForce : MonoBehaviour
 {
     float speed = 1.0f;
     Vector3 force;

     void Awake()
     {
         //Another way to "Require" the component.
         if(!GetComponent<ConstantForce>())
             gameObject.AddComponent<ConstantForce>();
         //
         force = GetComponent<ConstantForce>().force;
         GetComponent<Rigidbody>().useGravity = false;
     }

     void Update()
     {
         GetComponent<ConstantForce>().force = force;

         force += -transform.forward * speed * Time.deltaTime;
         /* Transform is negative because in my game,  I have the
            illusion of the objects coming down my mountain.
         */   
     }
 }

Solution

  • Usually, to do stuff like this, you need 2 variables:

    1. Time in which you want the speed to increase in. (For example, increase every 5 seconds) secondsToIncreaseIn.

    2. Value to increase by.(For example, increment speed by 2 every time in #1 seconds) valueToIncreaseBy.

    This way you have flexible ways to speed up or slow down how much your speed increments by changing either variable. With experiment setting secondsToIncreaseIn to 5 and valueToIncreaseBy to 0.0007f made the game last longer. You can increase valueToIncreaseBy to make it even last longer.You can change the startingSpeed variable if the starting speed is too slow or high.

    private float speed = 0f;
    private Vector3 force;
    private ConstantForce cachedConstantForce;
    private float counter = 0; //Dont change(Used for counting)
    
    public float startingSpeed = 5f;//Start with 5 speed
    public int secondsToIncreaseIn = 5; //Increase in every 5 seconds
    public float valueToIncreaseBy = 0.0007f; //Increase by 0.0007 in secondsToIncreaseIn time/seconds
    
    
    
    void Awake()
    {
        //Another way to "Require" the component.
        cachedConstantForce = gameObject.GetComponent<ConstantForce>();
    
        if (cachedConstantForce == null)
        {
            gameObject.AddComponent<ConstantForce>();
        }
    
    
        cachedConstantForce = GetComponent<ConstantForce>();
    }
    
    void Start()
    {
        force = cachedConstantForce.force;
        GetComponent<Rigidbody>().useGravity = false;
        speed = startingSpeed;
    }
    
    void Update()
    {
        cachedConstantForce.force = force;
    
        force = -transform.forward * speed * Time.deltaTime;
        /* Transform is negative because in my game,  I have the
           illusion of the objects coming down my mountain.
        */
    
        if (counter < secondsToIncreaseIn)
        {
            counter += Time.deltaTime;
        }
        else
        {
            //Time has reached to Increase value
            counter = 0; //Reset Timer
            //Increase Spped
            speed += valueToIncreaseBy;
    
            Debug.Log("Increased Speed!");
        }
    }