Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I increase the speed of obstacles in a dodging game overtime? How does it work?

Discussion in 'Scripting' started by skylitcoding, Dec 29, 2020.

  1. skylitcoding

    skylitcoding

    Joined:
    Dec 29, 2020
    Posts:
    2
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ObstacleDestroyer : MonoBehaviour
    4. {    
    5.     //Attach this script to a GameObject with a Renderer component attached
    6.     //If the GameObject is visible to the camera, the message is output to the console
    7.     //Every GameObject has a renderer to display it on the screen. This script will get the renderer
    8.     //of the Obstacle gameObject, and then, it will check if this renderer is visible in the main camera.
    9.     //if it is visible, the game object will be safe. or else, it will be deleted.
    10.     Renderer m_Renderer;
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.         m_Renderer = GetComponent<Renderer>();
    15.         GetComponent<ObstacleMovement>().backwardForce += Time.timeSinceLevelLoad * 2; // Why does this work?
    16.         GetComponent<ObstacleMovement>().backwardForce += 100;// Why does this not work?
    17.  
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (m_Renderer.IsVisibleFrom(Camera.main))
    24.         {
    25.             ;
    26.         }
    27.         else
    28.         {
    29.             Destroy(m_Renderer.gameObject);
    30.             Debug.Log(GetComponent<ObstacleMovement>().backwardForce.ToString());
    31.         }
    32.     }
    33. }
    I'm currently following a Brackeys tutorial on Dodge the Block game. For the system where the game speeds up over time, I tried adding the backwardForce (Which is the force applied to the obstacles for it to move backwards towards the player) but it didn't work. Apparently I had to add it to the time since the level loaded. Why does that work but just adding to it doesnt work? And why is it used in the start method instead of the update method?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,138
    Without knowing more of how backwardForce is applied, my first guess is the value of Time.timeSinceLevelLoad value when multiplied by 2 might be higher than just adding 100. But it might also be lower. Either way, you should see what value the first one is applying to determine what the difference is.
     
    skylitcoding likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    100 is constant over time.

    Time.timeSinceLevelLoad increases over time.

    It's that simple.
     
    Brathnann and skylitcoding like this.
  4. skylitcoding

    skylitcoding

    Joined:
    Dec 29, 2020
    Posts:
    2
    Oh, I see. THanks. I needed that confirmation. Love you!