Introducing a score limit and bugfixes

This commit is contained in:
2019-09-26 00:48:36 +02:00
parent 3499961a98
commit 4bb1730091
3 changed files with 525 additions and 24 deletions

View File

@@ -5,6 +5,7 @@ using UnityEngine;
public class ForceZone : MonoBehaviour
{
public float force = 0;
public GameObject player;
// Start is called before the first frame update
void Start()
{
@@ -19,8 +20,13 @@ public class ForceZone : MonoBehaviour
private void OnTriggerStay(Collider other)
{
Vector3 direction = other.transform.position - this.transform.position;
Vector3 direction = other.transform.position - this.player.transform.position;
Debug.DrawRay(this.transform.position, direction);
other.transform.GetComponent<Rigidbody>().AddForce(direction.normalized * this.force * Time.deltaTime, ForceMode.Impulse);
Rigidbody otherRigidbody = other.transform.GetComponent<Rigidbody>();
if (otherRigidbody)
{
other.transform.GetComponent<Rigidbody>().AddForce(direction.normalized * this.force * Time.deltaTime, ForceMode.Impulse);
}
}
}

View File

@@ -2,10 +2,12 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public int scorePlayer1 = 0;
public int scorePlayer2 = 0;
public int maxGoals = 1;
public Transform player1;
public Transform player2;
@@ -15,30 +17,65 @@ public class GameManager : MonoBehaviour
/* UI */
public Text scoreLabel;
public Text winLabel;
// Start is called before the first frame update
void Start()
{
this.player1Initial = new GameObject().transform;
this.player1Initial.position = this.player1.position;
this.player1Initial.rotation = this.player1.rotation;
this.player2Initial = new GameObject().transform;
this.player2Initial.position = this.player2.position;
this.player2Initial.rotation = this.player2.rotation;
this.ballInitial = new GameObject().transform;
this.ballInitial.position = this.ball.position;
winLabel.transform.gameObject.SetActive(false);
}
// Update is called once per frame
void Update()
{
this.scoreLabel.text = "Score: " + this.scorePlayer1 + " : " + this.scorePlayer2;
if (this.scorePlayer1 >= maxGoals)
{
winLabel.text = "Player 1 wins the game!";
winLabel.transform.gameObject.SetActive(true);
Time.timeScale = 0.0f;
}
if (this.scorePlayer2 >= maxGoals)
{
winLabel.text = "Player 2 wins the game!";
winLabel.transform.gameObject.SetActive(true);
Time.timeScale = 0.0f;
}
if (this.scorePlayer2 >= maxGoals || this.scorePlayer1 >= maxGoals)
{
if (Input.GetKeyDown(KeyCode.F5))
{
Time.timeScale = 1;
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}
public void ResetPositions()
{
this.player1.position = this.player1Initial.position;
this.player1.rotation = this.player1Initial.rotation;
this.player1.GetComponent<Rigidbody>().velocity = Vector3.zero;
this.player2.position = this.player2Initial.position;
this.player2.rotation = this.player2Initial.rotation;
this.player2.GetComponent<Rigidbody>().velocity = Vector3.zero;
this.ball.position = this.ballInitial.position;
this.ball.GetComponent<Rigidbody>().velocity = Vector3.zero;
}
}