The following is code from two separate classes I learned during space shooter tutorial, but I want more.
I want to set up the scroll speed of the back ground to almost look like warp style once the player reaches a certain score. I will post the code from both of my classes involved. With the current code, nothing happens once I have reach Score of 50 which takes only 6 hits of an asteroid.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour {
public GameObject[] hazards;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public GUIText scoreText;
public int score;
public GUIText restartText;
public GUIText gameOverText;
private bool gameOver;
private bool restart;
// Use this for initialization
void Start () {
StartCoroutine(SpawnWaves ());
score = 0;
UpdateScore();
gameOver = false;
restart = false;
restartText.text = "";
gameOverText.text = "";
}
// Update is called once per frame
void Update () {
if (restart)
{
if (Input.GetKeyDown (KeyCode.R))
{
SceneManager.LoadScene("Space Shooter Scene");
}
}
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
GameObject hazard = hazards[Random.Range(0, hazards.Length)];
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
if (gameOver)
{
restartText.text = "Press 'R' for game restart";
restart = true;
break;
}
}
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
public void GameOver ()
{
gameOverText.text = "Game Over";
gameOver = true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BG_Scroller : MonoBehaviour {
public float scrollSpeed;
public float tileSizeZ;
private Vector3 startPosition;
private GameController gameController;
// Use this for initialization
void Start () {
startPosition = transform.position;
gameController = gameController.GetComponent<GameController>();
}
// Update is called once per frame
void Update () {
float newPosition = Mathf.Repeat(Time.time * scrollSpeed, tileSizeZ );
transform.position = startPosition + Vector3.forward * newPosition;
if (gameController.scoreText.text == "Score: 50")
{
scrollSpeed = scrollSpeed * 50;
}
}
}
Youâre checking the score in an oddball way. Donât check the score text. Check the score variable (simply called âscoreâ - no quotes).
Youâre currently checking if the score is exactly 50 (or attempting to check that). Use >= to check if itâs greater than or equal to the score youâre checking.
less important, but Iâd do this check as the first part of your update, but itâs no big deal how you have it now⌠no one will notice the one frame you missed setting the speed for
The modified update of BG_Scroller:
// Update is called once per frame
void Update () {
if (gameController.score >= 50)
{
scrollSpeed = scrollSpeed * 50;
}
float newPosition = Mathf.Repeat(Time.time * scrollSpeed, tileSizeZ );
transform.position = startPosition + Vector3.forward * newPosition;
}
OK, Iâve posted the code above, but the error youâre seeing needs more details. You need to send us the full error. It will have a line number indicating what object variable you have not properly âlinkedâ to something (thatâs what that error means).
For example, If itâs the error being reported is line 10 of BG_Scroller, then what you do is click on the object in the hierarchy that has the BG_Scroller script attached to it. Next you drag in the GameController script onto the spot in the property inspector for âgameControllerâ. This associates this variable with that script.
So itâs telling you that the error is on line 22 of your BGScroller script. So look at line 22 and see whatâs being called. Youâve updated your script since you posted it initially, so Iâm not sure what you have there now. Post your updated BGSCroller script if you canât figure it out.
This would be your updated BGScroller.cs, I believe:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BG_Scroller : MonoBehaviour {
public float scrollSpeed;
public float tileSizeZ;
private Vector3 startPosition;
public GameController gameController;
// Use this for initialization
void Start () {
startPosition = transform.position;
}
// Update is called once per frame
void Update () {
if (gameController.score >= 50)
{
scrollSpeed = scrollSpeed * 50;
}
float newPosition = Mathf.Repeat(Time.time * scrollSpeed, tileSizeZ );
transform.position = startPosition + Vector3.forward * newPosition;
}
}
Donât forget to link your GameController script via the inspector!
The private to public change while dragging the game controller worked! Thanks. Now comes my next problem lol, it shoots the speed so fast the mathf.repeat doesnât seem to be able to keep up, and i lose my background lol. I was thinking of slowing the speed value to maybe 25 but just incase do you have any suggestions on how to adjust the script instead.
thank you for the all the help btw, I spent 4 years learning coding in college but not a single class worked with game engines, it only worked with IDEâs and flash
All good⌠Programming is still the same, in principle Learning the Engine is a bit more, for sure - part of the package.
As for your speed, Iâd try different speeds. Repeat should loop/wrap the valueâŚMaybe just adjust it in the inspector while the game is running & see what looks good?
EDIT: actually on that note, it looks like my scroll speed just said infinity now but 50 still worked ok, ill try and figure out why
The New error that popped up is this.
transform.position assign attempt for âBackgroundâ is not valid. Input position is { NaN, NaN, NaN }.
UnityEngine.Transform:set_position(Vector3)
BG_Scroller:Update() (at Assets/Scripts/BG_Scroller.cs:21)
Ok maybe the problem is you keep multiplying it.
like, whenever youâre at the stage to increase it , you keep multiplying it by 50 (based on some code above)