Question by
LarryHess · Aug 11, 2017 at 06:49 AM ·
speedbackgroundboxcolliderboxcollider2dscrolling
Increase speed when object hits collider ?
Hey everyone, I'm still learning Unity and C# and this is my first game I'm working on. Basically what I want is that once my plane hits a Box Collider, the scrolling speed of the background and the columns (obstacles that scroll as well) increases by a little bit.
Here's the Game Control code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameControl : MonoBehaviour {
public static GameControl instance;
public GameObject gameOverText;
public Text scoreText;
public bool gameOver = false;
public float speed = -5f;
private int score = 0;
// Use this for initialization
void Awake () {
if (instance == null) {
instance = this;
} else if (instance != this)
{
Destroy(gameObject);
}
}
// Update is called once per frame
void Update () {
if (gameOver == true && Input.GetMouseButtonDown (0))
{
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
}
}
public void PlaneScored()
{
if (gameOver)
{
return;
}
score++;
scoreText.text = "Score: " + score.ToString ();
}
public void PlaneDied()
{
gameOverText.SetActive (true);
gameOver = true;
}
}
And here's the code for the columns:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Column : MonoBehaviour {
public GameControl control;
private void OnTriggerEnter2D (Collider2D other)
{
if (other.GetComponent<plane> () != null) {
GameControl.instance.PlaneScored ();
}
}
}
And finally the scrolling object code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScrollingObject : MonoBehaviour {
public GameControl control;
private float speed = 0;
private Rigidbody2D rb2d;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
speed = control.speed;
rb2d.velocity = new Vector2(speed,0);
}
// Update is called once per frame
void Update () {
speed = control.speed;
rb2d.velocity = new Vector2(speed,0);
if (GameControl.instance.gameOver == true)
{
rb2d.velocity = Vector2.zero;
}
}
}
Any help is appreciated, thanks a lot !
Comment