Refactor position reset logic

This commit is contained in:
2019-10-04 23:53:38 +02:00
parent 7ac7e4fa89
commit 6e81cecb0a
2 changed files with 30 additions and 13 deletions

View File

@@ -19,6 +19,10 @@ public class GameManager : MonoBehaviour
public Transform player1Initial, player2Initial, ballInitial;
public GameObject[] players;
public Vector3[] initialPositions;
public Quaternion[] initialRotations;
/* UI */
public Text scoreLabel;
public Text winLabel;
@@ -27,6 +31,15 @@ public class GameManager : MonoBehaviour
// Start is called before the first frame update
void Start()
{
this.players = GameObject.FindGameObjectsWithTag("Player");
this.initialPositions = new Vector3[this.players.Length];
this.initialRotations = new Quaternion[this.players.Length];
for (int i = 0; i < this.players.Length; i++)
{
this.initialPositions[i] = this.players[i].transform.position;
this.initialRotations[i] = this.players[i].transform.rotation;
}
this.audioSource = GetComponent<AudioSource>();
this.player1Initial = new GameObject().transform;
this.player1Initial.position = this.player1.position;
@@ -78,15 +91,16 @@ public class GameManager : MonoBehaviour
this.audioSource.clip = this.goalSound;
this.audioSource.Play();
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;
for (int i = 0; i < this.players.Length; i++)
{
this.players[i].transform.position = this.initialPositions[i];
this.players[i].transform.rotation = this.initialRotations[i];
this.players[i].GetComponent<Rigidbody>().velocity = Vector3.zero;
}
this.ball.position = this.ballInitial.position;
this.ball.GetComponent<Rigidbody>().velocity = Vector3.zero;
}
}