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

@@ -455,7 +455,7 @@ GameObject:
- component: {fileID: 295107913} - component: {fileID: 295107913}
m_Layer: 0 m_Layer: 0
m_Name: AI Additional m_Name: AI Additional
m_TagString: Untagged m_TagString: Player
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
@@ -658,7 +658,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 295107912} m_GameObject: {fileID: 295107912}
m_LocalRotation: {x: 0, y: 0.7071068, z: 0, w: 0.7071068} m_LocalRotation: {x: 0, y: 0.7071068, z: 0, w: 0.7071068}
m_LocalPosition: {x: -31.8, y: 1, z: -5.7} m_LocalPosition: {x: -34.96, y: 1, z: 4.01}
m_LocalScale: {x: 2, y: 2, z: 2} m_LocalScale: {x: 2, y: 2, z: 2}
m_Children: m_Children:
- {fileID: 2145556007} - {fileID: 2145556007}
@@ -714,7 +714,7 @@ GameObject:
- component: {fileID: 367746335} - component: {fileID: 367746335}
m_Layer: 0 m_Layer: 0
m_Name: Player Blue m_Name: Player Blue
m_TagString: Untagged m_TagString: Player
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
@@ -1760,7 +1760,7 @@ GameObject:
- component: {fileID: 708460350} - component: {fileID: 708460350}
m_Layer: 0 m_Layer: 0
m_Name: Player Red m_Name: Player Red
m_TagString: Untagged m_TagString: Player
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
@@ -1988,7 +1988,7 @@ GameObject:
- component: {fileID: 929235632} - component: {fileID: 929235632}
m_Layer: 0 m_Layer: 0
m_Name: Player Additional m_Name: Player Additional
m_TagString: Untagged m_TagString: Player
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
@@ -2055,7 +2055,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 929235626} m_GameObject: {fileID: 929235626}
m_LocalRotation: {x: 0, y: -0.7071068, z: 0, w: 0.7071068} m_LocalRotation: {x: 0, y: -0.7071068, z: 0, w: 0.7071068}
m_LocalPosition: {x: 31, y: 1, z: 8.9} m_LocalPosition: {x: 35.02, y: 1, z: 3.8}
m_LocalScale: {x: 2, y: 2, z: 2} m_LocalScale: {x: 2, y: 2, z: 2}
m_Children: m_Children:
- {fileID: 966074800} - {fileID: 966074800}
@@ -2576,6 +2576,9 @@ MonoBehaviour:
player1Initial: {fileID: 0} player1Initial: {fileID: 0}
player2Initial: {fileID: 0} player2Initial: {fileID: 0}
ballInitial: {fileID: 0} ballInitial: {fileID: 0}
players: []
initialPositions: []
initialRotations: []
scoreLabel: {fileID: 697913066} scoreLabel: {fileID: 697913066}
winLabel: {fileID: 1880081387} winLabel: {fileID: 1880081387}
--- !u!4 &1228665502 --- !u!4 &1228665502

View File

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