I am trying to get the variable pipeMoveSpeed to increment slowly as the game progresses. It is the script component for a pipe prefab that gets instantiated by a spawner object. I am 100% sure there is an answer somewhere to this, but I have spent 2 days searching, and I don’t know what question to search to get an answer.
This is my best attempt, but the pipeMoveSpeed variable ramps up exponentially and I just want it to increment 1 unit every x seconds.
Here is the script for the pipe prefab:
using UnityEngine;
using UnityEngine.InputSystem.Controls;
public class PipeScript : MonoBehaviour
{
public float pipeMoveSpeed = 10;
public float deadZone = -45;
public LogicScript logic;//reference pipeIncrementer from logicScript in Logic Script Game Object
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
//Increment pipeMoveSpeed when pipeIncrementer increments
pipeMoveSpeed += GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>().pipeIncrementer;
//Debug.Log($"pipeMoveSpeed is: {pipeMoveSpeed.ToString()}");
//move pipe prefab at set rate based on pipeMoveSpeed at time of initialization
transform.position += (pipeMoveSpeed * Vector3.left * Time.deltaTime);
//call Destroy() function when pipes exit game screen
if (transform.position.x < deadZone)
{
Destroy();
}
}
//Destroy GameObject
void Destroy()
{
Debug.Log("Pipe Deleted");
Destroy(gameObject);
}
}
and here is the Logic Script:
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Runtime.CompilerServices;
public class LogicScript : MonoBehaviour
{
public int playerScore;
public Text Score;
public float gameClock;
public TMP_Text GameClock;
public float pipeIncrementer;
void Update()
{
float pipeTimer = gameClock % 5;
Debug.Log($"pipetimer: = {pipeTimer.ToString()}");
if (pipeTimer >= 1)
{
pipeIncrementer++;
Debug.Log($"pipeIncrementer Incremented to: {pipeIncrementer}");
pipeTimer = 0;
}
gameClock += Time.deltaTime;
int minutes = Mathf.FloorToInt(gameClock / 60F);
int seconds = Mathf.FloorToInt(gameClock - minutes * 60);
GameClock.text = string.Format("{0:0}:{1:00}", minutes, seconds);
}
[ContextMenu("Add Score")]
public void AddScore()
{
playerScore++;
Score.text = playerScore.ToString();
}
}