FaceoffSetupGuide.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using UnityEngine;
  2. /// <summary>
  3. /// SETUP GUIDE FOR FACEOFF SYSTEM
  4. ///
  5. /// 1. CREATE THE PUCK:
  6. /// - Create a GameObject named "Puck"
  7. /// - Add the PuckController script to it
  8. /// - Position it at your center ice faceoff spot
  9. /// - It will automatically create its own physics and visuals
  10. ///
  11. /// 2. TAG YOUR PLAYERS:
  12. /// - Select all home team players and set their tag to "HomeTeam"
  13. /// - Select all away team players and set their tag to "AwayTeam"
  14. /// - (Create these tags in Tags & Layers if they don't exist)
  15. ///
  16. /// 3. CREATE THE FACEOFF MANAGER:
  17. /// - Create an empty GameObject named "FaceoffManager"
  18. /// - Add the FaceoffManager script to it
  19. /// - Drag the Puck GameObject into the "Puck" field
  20. /// - The script will auto-find players by their tags
  21. ///
  22. /// 4. TESTING:
  23. /// - Press F key during play mode to trigger a faceoff
  24. /// - Watch the console for outcome messages
  25. ///
  26. /// 5. INTEGRATION WITH YOUR GAME:
  27. /// - Call faceoffManager.InitiateFaceoff(position) from your game logic
  28. /// - Or call faceoffManager.StartFaceoff() if puck is already positioned
  29. ///
  30. /// HOW THE SYSTEM WORKS:
  31. ///
  32. /// PUCK PHYSICS:
  33. /// - The puck has a Rigidbody with realistic hockey puck properties
  34. /// - Low friction simulates ice sliding
  35. /// - When a player is close enough, they automatically pick it up
  36. /// - Yellow ring indicator shows who has possession
  37. ///
  38. /// FACEOFF MECHANICS:
  39. /// - Compares center players' stats (strength, stick handling, awareness, agility)
  40. /// - Three possible outcomes:
  41. /// 1. HOME WIN: Home center wins cleanly, passes to defender
  42. /// 2. AWAY WIN: Away center wins cleanly, passes to defender
  43. /// 3. STRUGGLE: Puck bounces loose, both teams scramble for it
  44. ///
  45. /// POSSESSION INDICATOR:
  46. /// - Rotating yellow ring appears around player with puck
  47. /// - Makes it instantly clear who controls the puck
  48. ///
  49. /// PLAYER ACTIONS WITH PUCK:
  50. /// - Shoot(direction, power): Fire the puck with physics
  51. /// - Pass(targetPlayer): Pass to a teammate
  52. /// - Check(targetPlayer): Hit opponent to knock puck loose
  53. ///
  54. /// </summary>
  55. public class FaceoffSetupGuide : MonoBehaviour
  56. {
  57. [Header("Quick Setup")]
  58. [Tooltip("Drag your puck GameObject here")]
  59. public GameObject puckObject;
  60. [Tooltip("Drag all home team players here")]
  61. public GameObject[] homeTeamPlayers;
  62. [Tooltip("Drag all away team players here")]
  63. public GameObject[] awayTeamPlayers;
  64. [Header("Auto-Setup Options")]
  65. [SerializeField] private bool autoCreatePuck = true;
  66. [SerializeField] private Vector3 puckStartPosition = new Vector3(0, 0.5f, 0);
  67. [ContextMenu("Auto-Setup Faceoff System")]
  68. public void AutoSetup()
  69. {
  70. Debug.Log("=== Starting Auto-Setup ===");
  71. // Step 1: Create or find puck
  72. if (puckObject == null && autoCreatePuck)
  73. {
  74. puckObject = new GameObject("Puck");
  75. puckObject.transform.position = puckStartPosition;
  76. puckObject.AddComponent<PuckController>();
  77. Debug.Log("✓ Created Puck GameObject");
  78. }
  79. else if (puckObject != null)
  80. {
  81. if (puckObject.GetComponent<PuckController>() == null)
  82. {
  83. puckObject.AddComponent<PuckController>();
  84. }
  85. Debug.Log("✓ Puck found and configured");
  86. }
  87. // Step 2: Tag home team players
  88. foreach (GameObject player in homeTeamPlayers)
  89. {
  90. if (player != null)
  91. {
  92. player.tag = "HomeTeam";
  93. }
  94. }
  95. Debug.Log($"✓ Tagged {homeTeamPlayers.Length} home team players");
  96. // Step 3: Tag away team players
  97. foreach (GameObject player in awayTeamPlayers)
  98. {
  99. if (player != null)
  100. {
  101. player.tag = "AwayTeam";
  102. }
  103. }
  104. Debug.Log($"✓ Tagged {awayTeamPlayers.Length} away team players");
  105. // Step 4: Create or configure FaceoffManager
  106. FaceoffManager manager = FindObjectOfType<FaceoffManager>();
  107. if (manager == null)
  108. {
  109. GameObject managerObj = new GameObject("FaceoffManager");
  110. manager = managerObj.AddComponent<FaceoffManager>();
  111. Debug.Log("✓ Created FaceoffManager");
  112. }
  113. // Use reflection to set the puck reference
  114. var puckField = typeof(FaceoffManager).GetField("puck",
  115. System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
  116. if (puckField != null && puckObject != null)
  117. {
  118. PuckController puck = puckObject.GetComponent<PuckController>();
  119. puckField.SetValue(manager, puck);
  120. Debug.Log("✓ Linked puck to FaceoffManager");
  121. }
  122. Debug.Log("=== Auto-Setup Complete! ===");
  123. Debug.Log("Press F during play mode to test the faceoff");
  124. }
  125. [ContextMenu("Validate Setup")]
  126. public void ValidateSetup()
  127. {
  128. Debug.Log("=== Validating Setup ===");
  129. int issues = 0;
  130. // Check for puck
  131. PuckController puck = FindObjectOfType<PuckController>();
  132. if (puck == null)
  133. {
  134. Debug.LogError("✗ No PuckController found in scene!");
  135. issues++;
  136. }
  137. else
  138. {
  139. Debug.Log("✓ Puck found");
  140. }
  141. // Check for FaceoffManager
  142. FaceoffManager manager = FindObjectOfType<FaceoffManager>();
  143. if (manager == null)
  144. {
  145. Debug.LogError("✗ No FaceoffManager found in scene!");
  146. issues++;
  147. }
  148. else
  149. {
  150. Debug.Log("✓ FaceoffManager found");
  151. }
  152. // Check for tagged players
  153. GameObject[] homePlayers = GameObject.FindGameObjectsWithTag("HomeTeam");
  154. GameObject[] awayPlayers = GameObject.FindGameObjectsWithTag("AwayTeam");
  155. if (homePlayers.Length == 0)
  156. {
  157. Debug.LogWarning("⚠ No players tagged as 'HomeTeam'");
  158. issues++;
  159. }
  160. else
  161. {
  162. Debug.Log($"✓ Found {homePlayers.Length} home team players");
  163. }
  164. if (awayPlayers.Length == 0)
  165. {
  166. Debug.LogWarning("⚠ No players tagged as 'AwayTeam'");
  167. issues++;
  168. }
  169. else
  170. {
  171. Debug.Log($"✓ Found {awayPlayers.Length} away team players");
  172. }
  173. // Check for centers
  174. bool homeCenter = false;
  175. bool awayCenter = false;
  176. foreach (var player in homePlayers)
  177. {
  178. PlayerController pc = player.GetComponent<PlayerController>();
  179. if (pc != null && pc.stats != null && pc.stats.position == PlayerPosition.C)
  180. {
  181. homeCenter = true;
  182. break;
  183. }
  184. }
  185. foreach (var player in awayPlayers)
  186. {
  187. PlayerController pc = player.GetComponent<PlayerController>();
  188. if (pc != null && pc.stats != null && pc.stats.position == PlayerPosition.C)
  189. {
  190. awayCenter = true;
  191. break;
  192. }
  193. }
  194. if (!homeCenter)
  195. {
  196. Debug.LogError("✗ No home team Center found!");
  197. issues++;
  198. }
  199. else
  200. {
  201. Debug.Log("✓ Home team Center found");
  202. }
  203. if (!awayCenter)
  204. {
  205. Debug.LogError("✗ No away team Center found!");
  206. issues++;
  207. }
  208. else
  209. {
  210. Debug.Log("✓ Away team Center found");
  211. }
  212. Debug.Log("=== Validation Complete ===");
  213. if (issues == 0)
  214. {
  215. Debug.Log("<color=green>✓ Everything looks good! Ready for faceoff!</color>");
  216. }
  217. else
  218. {
  219. Debug.Log($"<color=red>Found {issues} issue(s) that need attention</color>");
  220. }
  221. }
  222. }