using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; public class Ball : MonoBehaviour { public Rigidbody2D rb; public float MaxDistance = 3f; public Rigidbody2D Hook; public GameObject NextLevel; public float releaseTime =0.15f; private bool isPressed = false; private void Update() { if(isPressed) { Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); if(Vector2. Distance(mousePos , Hook . transform.position) < MaxDistance) { rb.position = mousePos; } else { Vector2 dir = (mousePos-Hook.position).normalized; rb.position = Hook.position + (dir * MaxDistance); } } } private void OnMouseDown() { isPressed = true; rb.isKinematic = true; } private void OnMouseUp() { isPressed = false; rb.isKinematic = false; StartCoroutine(Release()); } IEnumerator Release () { yield return new WaitForSeconds(releaseTime); SpringJoint2D hook = GetComponent().connectedBody.GetComponent(); hook.enabled=false; GetComponent().enabled = false; this.enabled = false; yield return new WaitForSeconds(2f); if(NextLevel != null) { NextLevel.SetActive(true); hook.connectedBody = NextLevel.GetComponent(); hook.enabled = true; }else { Debug.Log("restaring"); Enemy.EnemiesAlive = 0; SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } } }