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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

issue with speeding up quad set on repeat

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

  1. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    So i have a quad with shader set to cutout that acts as a foreground element on an endless runner game. the game is set to speed up after every 100 points which works fine but my quad gets reset every time i hit the points. like on 100 it flickers back to its starting position and speeds up as expected. any ideas how i can make it seamlessly? the code is not anything special to be honest just multiple if statements

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TextureScroll : MonoBehaviour
    6. {
    7.     Material backgroundMaterial;
    8.     public float scrollSpeed;
    9.     public bool scroll = true;
    10.     private void Awake()
    11.     {
    12.         backgroundMaterial = GetComponent<Renderer>().material;
    13.     }
    14.     // Start is called before the first frame update
    15.     void Update()
    16.     {
    17.        
    18.         if (GameManager.score > 100)
    19.         {
    20.             scrollSpeed = 0.10f;
    21.         }
    22.         if (GameManager.score > 200)
    23.         {
    24.             scrollSpeed = 0.12f;
    25.         }
    26.         if (GameManager.score > 300)
    27.         {
    28.             scrollSpeed = 0.14f;
    29.         }
    30.         if (GameManager.score > 400)
    31.         {
    32.             scrollSpeed = 0.16f;
    33.         }
    34.         if (GameManager.score > 500)
    35.         {
    36.             scrollSpeed = 0.18f;
    37.         }
    38.         if (GameManager.score > 600)
    39.         {
    40.             scrollSpeed = 0.20f;
    41.         }
    42.        
    43.     }
    44.  
    45.     private void FixedUpdate()
    46.     {
    47.         if (scroll)
    48.             {
    49.                 Vector2 offset = new Vector2(scrollSpeed * Time.time,0);
    50.                 backgroundMaterial.mainTextureOffset = offset;
    51.             }
    52.     }
    53. }
    54.  
    edit i can do it like this adding an acceleration variable for it to speed up over time but i am also trying to figure out how to do it with score too!
    Code (CSharp):
    1. scrollSpeed += Time.deltaTime * acceleration;
     
    Last edited: Dec 29, 2020
  2. luke_2

    luke_2

    Joined:
    Nov 20, 2012
    Posts:
    29
    I haven't tried this to check, but it looks like the changing of the scrollSpeed variable will cause the offset to suddenly jump.

    Would suggest making offset a class variable.
    i.e. between lines 9 and 10 add:
    Vector2 offset = Vector2.zero;

    Then at line 42 add (this ensures the offset is continuously amended rather than suddenly jumping when scroll speed changes):
    offset.x+=scrollSpeed*Time.deltaTime;

    then remove line 49 as the offset has already been calculated.
     
    Kurt-Dekker likes this.