using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScrollingObject : MonoBehaviour
{
private Rigidbody2D rb2d;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
//if (GameControl.instance.score > 0)
// rb2d.velocity = new Vector2(GameControl.instance.scrollSpeed * GameControl.instance.score, 0);
//else
rb2d.velocity = new Vector2(GameControl.instance.scrollSpeed, 0);
}
// Update is called once per frame
void Update()
{
if (GameControl.instance.score > 5)
rb2d.velocity = new Vector2(GameControl.instance.scrollSpeed *3, 0);
if (GameControl.instance.score > 10)
rb2d.velocity = new Vector2(GameControl.instance.scrollSpeed*5, 0);
if (GameControl.instance.gameOver == true)
{
rb2d.velocity = Vector2.zero;
}
}
}
CodePudding user response:
You already seem to have a central Singleton controller storing your values.
So instead of poll checking these values every frame I would rather implement it event based using properties and an event
public class GameControl : MonoBehaviour
{
// by default multiplier is 1
private float multiplier = 1;
// Configure here whatever your scrollSpeed is anyway
[SerializeField] private float _scrollSpeed = 20;
// A public read-only property which already has the multiplier applied
public float scrolSpeed => _scrollSpeed * multiplier;
// the actual score backing field
private int _score;
public int score
{
// when accessing the score property just return the internal _score value
get => _score;
// when assigning a new value to score
set
{
// update the backing field
_score = value;
// also update the multiplier
// use a base value of 1
// intentionally use an int division to increase multiplier by 2 for every 5 points
multiplier = 1 2 * score / 5;
// inform listeners that the score was changed
onScoreChanged?.Invoke();
}
}
// evnt whih is invoked everytime the score is changed
public event Action onScoreChanged;
...
}
and then you simply want to do
public class ScrollingObject : MonoBehaviour
{
[SerializeField] private Rigidbody2D rb2d;
private void Awake()
{
if(!rb2d) rb2d = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
rb2d.velocity = Vector2.right * GameControl.instance.scrollSpeed;
}
}
if you really need to constantly set the velocity for whatever reason.
Or even better do it all event based and only update the velocity once the score was actually changed:
public class ScrollingObject : MonoBehaviour
{
[SerializeField] private Rigidbody2D rb2d;
private void Awake()
{
if(!rb2d) rb2d = GetComponent<Rigidbody2D>();
// fetch the initial values once immediately
OnScoreChanged();
// attach a callback to the score changed event
GameControl.instance.onScoreChanged = OnScoreChanged;
}
private void OnDestroy()
{
// be sure to remove callbacks as soon as not needed anymore
GameControl.instance.onScoreChanged = OnScoreChanged;
}
// automatically called once every time the score was changed
private void OnScoreChanged()
{
// update the velocity
rb2d.velocity = Vector2.right * GameControl.instance.scrollSpeed;
}
}
CodePudding user response:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColumnPool : MonoBehaviour
{
public int columnPoolSize = 5;
public GameObject columnPrefab;
public float spawnRate = 1f;
public float columnMin = -1.5f;
public float columnMax = 1.5f;
private GameObject[] columns;
private Vector2 objectPoolPosition = new Vector2(-15f, -25f);
private float timeSinceLastSpawned;
private float spawnXPosition = 10f;
private int currentColumn = 0;
// Start is called before the first frame update
void Start()
{
columns = new GameObject[columnPoolSize];
for (int i = 0; i < columnPoolSize; i )
{
columns[i] = (GameObject)Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);
}
}
// Update is called once per frame
void Update()
{
// timeSinceLastSpawned = Time.deltaTime;
//if (GameControl.instance.score > 5)
//timeSinceLastSpawned = Time.deltaTime * GameControl.instance.score;
//// timeSinceLastSpawned = Time.deltaTime;
//if (GameControl.instance.score % 5 == 0)
if ((GameControl.instance.score > 1 && GameControl.instance.score % 5 == 0))
{
timeSinceLastSpawned = Time.deltaTime;
}
// timeSinceLastSpawned = Time.deltaTime *1.0f;
//if (GameControl.instance.score > 5)
// timeSinceLastSpawned = Time.deltaTime * 2;
if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
{
timeSinceLastSpawned = 0;
float spawnYPosition = Random.Range(columnMin, columnMax);
columns [currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
currentColumn ;
if (currentColumn >= columnPoolSize)
{
currentColumn = 0;
}
}
}
}