Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BallSpawner : MonoBehaviour
- {
- [SerializeField] private GameObject _ball;
- [SerializeField] private Transform _leftBound;
- [SerializeField] private Transform _rightBound;
- [Space]
- [Range(0, 10)]
- [SerializeField] private float _dropPower = 1f;
- private GameObject _instantiatedBall;
- private Rigidbody _ballRigidbody;
- private Transform _ballTransform;
- private bool _dropped = false;
- public void ResetBall()
- {
- UpdateTransform();
- _ballRigidbody.isKinematic = true;
- _dropped = false;
- }
- private void Start()
- {
- Vector3 initPosition = GetInitPosition();
- _instantiatedBall = Instantiate(_ball, initPosition, Quaternion.identity);
- _ballRigidbody = _instantiatedBall.GetComponent<Rigidbody>();
- _ballTransform = _instantiatedBall.transform;
- _ballRigidbody.isKinematic = true;
- _dropped = false;
- }
- private void DropBall()
- {
- if (_dropped == true) return;
- _ballRigidbody.isKinematic = false;
- _ballRigidbody.AddForce(_ballTransform.forward * _dropPower * 1000f);
- _dropped = true;
- }
- private void UpdateTransform()
- {
- Vector3 initPosition = GetInitPosition();
- _ballTransform.position = initPosition;
- _ballTransform.rotation = Quaternion.identity;
- }
- private Vector3 GetInitPosition()
- {
- float mouseX = Input.mousePosition.x;
- float relativeValue = mouseX / Screen.width;
- return Vector3.Lerp(_leftBound.position, _rightBound.position, relativeValue);
- }
- private void Update()
- {
- if (_dropped == true) return;
- UpdateTransform();
- if (Input.GetMouseButtonDown(0)) DropBall();
- }
- }
- //------------------------------------------------------------------------------------------------------
- public class KeglesSpawner : MonoBehaviour
- {
- [SerializeField] private GameObject _keglSource;
- [SerializeField] private Transform[] _spawnPoints;
- private GameObject[] _kegles;
- public void ResetKegles()
- {
- for (int i = 0; i < _spawnPoints.Length; i++)
- {
- Transform child = _kegles[i].transform.GetChild(0);
- Rigidbody childRigidbody = child.GetComponent<Rigidbody>();
- childRigidbody.MovePosition(_spawnPoints[i].position + new Vector3(0, 1.25f, 0));
- childRigidbody.MoveRotation(Quaternion.Euler(-90, 0, 0));
- }
- }
- private void Start()
- {
- Spawn();
- }
- private void Spawn()
- {
- if (_kegles != null) throw new Exception("You can't to create new Kegles");
- _kegles = new GameObject[_spawnPoints.Length];
- for(int i = 0; i < _spawnPoints.Length; i++)
- {
- _kegles[i] = Instantiate(_keglSource, _spawnPoints[i].position, Quaternion.identity);
- }
- }
- }
- //---------------------------------------------------------------------------------------------------
- public class ReturnFinish : MonoBehaviour
- {
- [SerializeField] private BallSpawner _ballSpawner;
- [SerializeField] private KeglesSpawner _keglesSpawner;
- private void OnTriggerStay(Collider other)
- {
- if(other.gameObject.tag == "Player")
- {
- StartCoroutine(AllReset());
- }
- }
- private IEnumerator AllReset()
- {
- yield return new WaitForSeconds(1);
- _ballSpawner.ResetBall();
- _keglesSpawner.ResetKegles();
- }
- }
- //-----------------------------------------------------------------------------------------------
- public class ReturnForce : MonoBehaviour
- {
- [SerializeField] private float _forcePower;
- private void OnTriggerStay(Collider other)
- {
- if(other.gameObject.tag == "Player")
- {
- Rigidbody rb = other.gameObject.GetComponent<Rigidbody>();
- rb.AddForce(Vector3.back * _forcePower);
- //Debug.Log("Trigger Stay");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement