//========================================================================================== // public methods //========================================================================================== public void AnchorExtremity(AbstractAnchorScript anchor, float breakForce) { _anchor = anchor; if (_hingeJoint == null) { _hingeJoint = this.gameObject.AddComponent <HingeJoint2D>(); } _hingeJoint.connectedBody = anchor.GetComponent <Rigidbody2D>(); if (anchor.GetComponent <MovingAnchorScript>() || anchor.GetComponent <LianeAnchor>()) { _hingeJoint.breakForce = breakForce; //On change la breakforce uniquement si l'anchor est un moving anchor } else if (_hingeJoint.breakForce != Mathf.Infinity) { _hingeJoint.breakForce = 99999; //Faire _hingJoint.breakForce = Mathf.Infinity } _hingeJoint.enabled = true; _anchor.IsInUse = true; IsAnchored = true; }
// The code for moving the extremities is in the fixedUpdate method because the unity manual recommends using for moving rigidbodies void FixedUpdate() { if (_isDead) { return; } //****************************************************** // DRAG AND DROP MOVEMENT if (Input.GetMouseButton(0)) { //********************** // mouse down AND dragging if (_isDragging /*&& _anchoringAllowed*/) { Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePos.z = 0; Vector3 force = mousePos - _draggedExtremity.transform.position; if (force.magnitude > 1) { force.Normalize(); } _draggedExtremity.GetComponent <Rigidbody2D>().AddForce(force * _draggingMultiplyer); } //********************** // mouse down AND NOT dragging else { //Collider2D[] colliders = Physics2D.OverlapPointAll(Camera.main.ScreenToWorldPoint(Input.mousePosition)); Collider2D[] colliders = Physics2D.OverlapCircleAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), 0.2f); if (colliders != null) { foreach (Collider2D collider in colliders) { if (collider.CompareTag("Extremity")) { ExtremityScript extremityScript = collider.GetComponent <ExtremityScript>(); _draggedExtremity = extremityScript; _draggedExtremity.UnanchorExtremity(); _draggedExtremity.IsMoving = true; _isDragging = true; _anchoringAllowed = false; Invoke("AllowAnchoring", 0.1f); break; } } } } } //********************** // mouse NOT down else { if (_isDragging && _anchoringAllowed) { Collider2D[] colliders = Physics2D.OverlapBoxAll(_draggedExtremity.transform.position, _draggedExtremity.GetComponent <BoxCollider2D>().size, _draggedExtremity.transform.rotation.z); if (colliders != null) { foreach (Collider2D collider in colliders) { AbstractAnchorScript anchorScript = collider.GetComponent <AbstractAnchorScript>(); if (collider.CompareTag("Anchor") && anchorScript.IsInUse == false) { //Debug.Log("end of drag, anchored extremity"); _currentChunkName = anchorScript.transform.parent.parent.name; _draggedExtremity.AnchorExtremity(anchorScript, _anchorBreakForce); GameObject fx = Instantiate(_grabFxPrefab); fx.transform.position = anchorScript.transform.position; if (!anchorScript.usedOnce) { anchorScript.usedOnce = true; UIManager.Instance.AddScore(_grabScoreBonus); } _levitationTimer = 0; EventManager.Instance.SendOnAnchorGrabbedEvent(); break; } } } } if (_isDragging) { _draggedExtremity.IsMoving = false; _draggedExtremity = null; _isDragging = false; } } }