Advertisement
sergezhu

Bowling

Dec 5th, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1. public class BallSpawner : MonoBehaviour
  2. {
  3.     [SerializeField] private GameObject _ball;
  4.     [SerializeField] private Transform _leftBound;
  5.     [SerializeField] private Transform _rightBound;
  6.    
  7.     [Space]
  8.     [Range(0, 10)]
  9.     [SerializeField] private float _dropPower = 1f;
  10.  
  11.     private GameObject _instantiatedBall;
  12.     private Rigidbody _ballRigidbody;
  13.     private Transform _ballTransform;
  14.     private bool _dropped = false;
  15.  
  16.     public void ResetBall()
  17.     {
  18.         UpdateTransform();
  19.         _ballRigidbody.isKinematic = true;
  20.         _dropped = false;
  21.     }
  22.  
  23.     private void Start()
  24.     {
  25.         Vector3 initPosition = GetInitPosition();
  26.  
  27.         _instantiatedBall = Instantiate(_ball, initPosition, Quaternion.identity);
  28.         _ballRigidbody = _instantiatedBall.GetComponent<Rigidbody>();
  29.         _ballTransform = _instantiatedBall.transform;
  30.         _ballRigidbody.isKinematic = true;
  31.         _dropped = false;
  32.     }
  33.  
  34.     private void DropBall()
  35.     {
  36.         if (_dropped == true) return;
  37.  
  38.         _ballRigidbody.isKinematic = false;
  39.         _ballRigidbody.AddForce(_ballTransform.forward * _dropPower * 1000f);
  40.  
  41.         _dropped = true;
  42.     }
  43.  
  44.     private void UpdateTransform()
  45.     {
  46.         Vector3 initPosition = GetInitPosition();
  47.  
  48.         _ballTransform.position = initPosition;
  49.         _ballTransform.rotation = Quaternion.identity;
  50.     }
  51.  
  52.     private Vector3 GetInitPosition()
  53.     {
  54.         float mouseX = Input.mousePosition.x;
  55.         float relativeValue = mouseX / Screen.width;
  56.  
  57.         return Vector3.Lerp(_leftBound.position, _rightBound.position, relativeValue);
  58.     }
  59.  
  60.     private void Update()
  61.     {
  62.         if (_dropped == true) return;
  63.  
  64.         UpdateTransform();
  65.  
  66.         if (Input.GetMouseButtonDown(0)) DropBall();
  67.     }
  68. }
  69.  
  70. //------------------------------------------------------------------------------------------------------
  71. public class KeglesSpawner : MonoBehaviour
  72. {
  73.     [SerializeField] private GameObject _keglSource;
  74.     [SerializeField] private Transform[] _spawnPoints;
  75.  
  76.     private GameObject[] _kegles;
  77.  
  78.     public void ResetKegles()
  79.     {
  80.         for (int i = 0; i < _spawnPoints.Length; i++)
  81.         {
  82.             Transform child = _kegles[i].transform.GetChild(0);
  83.             Rigidbody childRigidbody = child.GetComponent<Rigidbody>();
  84.  
  85.             childRigidbody.MovePosition(_spawnPoints[i].position + new Vector3(0, 1.25f, 0));
  86.             childRigidbody.MoveRotation(Quaternion.Euler(-90, 0, 0));
  87.         }
  88.     }
  89.     private void Start()
  90.     {
  91.         Spawn();
  92.     }
  93.  
  94.     private void Spawn()
  95.     {
  96.         if (_kegles != null) throw new Exception("You can't to create new Kegles");
  97.  
  98.         _kegles = new GameObject[_spawnPoints.Length];
  99.        
  100.         for(int i = 0; i < _spawnPoints.Length; i++)
  101.         {
  102.             _kegles[i] = Instantiate(_keglSource, _spawnPoints[i].position, Quaternion.identity);
  103.         }
  104.     }    
  105. }
  106.  
  107.  
  108. //---------------------------------------------------------------------------------------------------
  109. public class ReturnFinish : MonoBehaviour
  110. {
  111.     [SerializeField] private BallSpawner _ballSpawner;
  112.     [SerializeField] private KeglesSpawner _keglesSpawner;
  113.  
  114.     private void OnTriggerStay(Collider other)
  115.     {
  116.         if(other.gameObject.tag == "Player")
  117.         {
  118.             StartCoroutine(AllReset());
  119.         }
  120.     }
  121.  
  122.     private IEnumerator AllReset()
  123.     {
  124.         yield return new WaitForSeconds(1);
  125.  
  126.         _ballSpawner.ResetBall();
  127.         _keglesSpawner.ResetKegles();
  128.     }
  129. }
  130.  
  131.  
  132. //-----------------------------------------------------------------------------------------------
  133. public class ReturnForce : MonoBehaviour
  134. {
  135.     [SerializeField] private float _forcePower;
  136.  
  137.     private void OnTriggerStay(Collider other)
  138.     {
  139.         if(other.gameObject.tag == "Player")
  140.         {
  141.             Rigidbody rb = other.gameObject.GetComponent<Rigidbody>();
  142.             rb.AddForce(Vector3.back * _forcePower);
  143.  
  144.             //Debug.Log("Trigger Stay");
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement