private void startGrap(Vector3 input) { RaycastHit2D hit; Vector3 shootDirection; shootDirection = Camera.main.ScreenToWorldPoint(input); shootDirection = shootDirection - this.gameObject.transform.position; shootDirection.z = 0; hit = Physics2D.Raycast(weapon.transform.position, shootDirection, maxDistanceGrapple, whatCanWeGrapple); try { if (hit != null && hit.collider.gameObject.GetComponent <Rigidbody2D> () != null) { grapplePoint = hit.point; joint = this.gameObject.AddComponent <SpringJoint2D> (); joint.autoConfigureConnectedAnchor = false; joint.connectedBody = hit.collider.gameObject.GetComponent <Rigidbody2D> (); joint.connectedAnchor = hit.point - new Vector2(hit.collider.transform.position.x, hit.collider.transform.position.y); joint.distance = distanceBetweenObjects; joint.dampingRatio = jointDampingRatio; isHanged = true; line.positionCount = 2; currentGrapplePosition = weapon.transform.position; hit.collider.GetComponent <TargetManager> ().GivePoint(); if (hit.collider.tag == "ExplodeTarget") { explotion = hit.collider.gameObject.GetComponent <TargetExplotion> (); explotion.playerHanged = true; explotion.StartCoroutine(explotion.StartExplotion()); } } } catch (NullReferenceException ex) { Debug.Log("Catched exception: " + ex); return; } }
private void HookShot() { #region WinCompiler #if UNITY_EDITOR_WIN if (Input.GetMouseButtonDown(0)) { startGrap(Input.mousePosition); } else if (Input.GetMouseButtonUp(0)) { if (explotion != null) { explotion.playerHanged = false; explotion = null; } stopGrap(); } #endif #endregion #region Android #if UNITY_ANDROID if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); switch (touch.phase) { case TouchPhase.Began: Vector3 pos; //touc.position returns a Vector2, so i addapted it as Vector3 pos = new Vector3(touch.position.x, touch.position.y, 0f); startPosition = pos; startGrap(pos); break; case TouchPhase.Moved: //impulse if (isHanged) { Vector3 newPos = new Vector3(touch.position.x, touch.position.y, 0f); float swipeValue = startPosition.x - newPos.x; float nwimfrce = 0; if (Mathf.Abs(swipeValue) > Mathf.Abs(impulseTrh)) { if (swipeValue > 0.1) { nwimfrce = -impulseForce; } else if (swipeValue < -0.1) { nwimfrce = impulseForce * 1.35f; } rb.AddForce(Vector2.right * nwimfrce, ForceMode2D.Impulse); } startPosition = newPos; } break; case TouchPhase.Ended: // releas target stopGrap(); break; } } #endif #endregion }