From bf938e5307d12e159b2132c4becd362b28752bfd Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Sun, 20 Mar 2022 21:21:58 +0200 Subject: [PATCH] fix: randomize start position in every game --- Assets/_Scripts/PlayerScripts/PlayerController.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Assets/_Scripts/PlayerScripts/PlayerController.cs b/Assets/_Scripts/PlayerScripts/PlayerController.cs index 9e1fdc8..8a3b411 100644 --- a/Assets/_Scripts/PlayerScripts/PlayerController.cs +++ b/Assets/_Scripts/PlayerScripts/PlayerController.cs @@ -1,4 +1,6 @@ +using System; using UnityEngine; +using Random = UnityEngine.Random; public class PlayerController : MonoBehaviour { @@ -30,13 +32,14 @@ public class PlayerController : MonoBehaviour _initialPositionY = _startPosition.y; _initialGravityScale = _rigidbody.gravityScale; - // Calculate start position (target position of ball spawning animation) - _startPosition = new Vector2(_startPosition.x + Random.Range(-_horisontalRadius, _horisontalRadius), _startPosition.y); - transform.position = new Vector2(_startPosition.x, transform.position.y); - GameStateManager.Instance.OnGameStateChange += OnGameStateChange; } + private void OnDestroy() + { + GameStateManager.Instance.OnGameStateChange -= OnGameStateChange; + } + public void OnTouch(Vector2 pointerPos) { // Get bounce direction based on the tap position @@ -67,6 +70,9 @@ public class PlayerController : MonoBehaviour _rigidbody.simulated = false; _rigidbody.velocity = Vector2.zero; _rigidbody.angularVelocity = 0; + + // Calculate start position (target position of ball spawning animation) + _startPosition = new Vector2(_startPosition.x + Random.Range(-_horisontalRadius, _horisontalRadius), _startPosition.y); break; case GameState.Game: _rigidbody.simulated = true;