using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class AsteroidControl : MonoBehaviour { public LineRenderer catapultLineFront; public LineRenderer catapultLineBack; SpringJoint2D spring; Vector2 prevVelocity; Ray leftCatpultToProjectile; bool clickedOn; void OnMouseDown() { clickedOn = true; } void OnMouseUp() { clickedOn = false; GetComponent().isKinematic = false; } void Awake() { spring = GetComponent(); } // Start is called before the first frame update void Start() { LineRendererSetup(); leftCatpultToProjectile = new Ray(catapultLineFront.transform.position, Vector3.zero); } void LineRendererSetup() { catapultLineFront.SetPosition(0, catapultLineFront.transform.position); catapultLineBack.SetPosition(0, catapultLineBack.transform.position); catapultLineFront.sortingLayerName = "Foreground"; catapultLineBack.sortingLayerName = "Foreground"; catapultLineFront.sortingOrder = 3; catapultLineBack.sortingOrder = 1; } // Update is called once per frame void Update() { if (clickedOn) { Dragging(); } if (spring != null) { if (!GetComponent().isKinematic && prevVelocity.sqrMagnitude>GetComponent().velocity.sqrMagnitude) { Destroy(spring); GetComponent().velocity = prevVelocity; } } else { catapultLineFront.enabled = false; catapultLineBack.enabled = false; } prevVelocity = GetComponent().velocity; LineRendererUpdate(); } void LineRendererUpdate() { Vector2 catpultToProjectile = transform.position - catapultLineFront.transform.position; Vector3 holdPoint = transform.position; catapultLineFront.SetPosition(1, holdPoint); catapultLineBack.SetPosition(1, holdPoint); } void Dragging() { Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); mouseWorldPoint.z = 0f; transform.position = mouseWorldPoint; } }