void OnTriggerEnter2D(Collider2D col) { if (col.tag == "Rope" && !sticked) { if (connectedRopeNode) { lastRopeSystem = connectedRopeNode.GetComponentInParent <RopeSystem>(); } connectedRopeNode = col.GetComponent <RopeNode>(); if (!connectedRopeNode.GetComponentInParent <RopeSystem>().enable) { return; } connectedRopeNode.GetComponent <Rigidbody2D>().mass += rigidBody.mass; forceToRope(rigidBody.velocity.x); boyTransform.parent = connectedRopeNode.transform; lerpPosition = 0; rigidBody.isKinematic = true; sticked = true; animator.SetBool("ClimbRope", true); if (humanController.pushing) { humanController.unPushObject(); } } }
public void climbDown() { if (animator.GetInteger("ClimbRopeDirection") != -1 && connectedRopeNode.nextNode) { connectedRopeNode.GetComponent <Rigidbody2D>().mass -= rigidBody.mass; connectedRopeNode = connectedRopeNode.nextNode; connectedRopeNode.GetComponent <Rigidbody2D>().mass += rigidBody.mass; boyTransform.parent = connectedRopeNode.transform; animator.SetInteger("ClimbRopeDirection", -1); } }
private void climbToRopeNode(RopeNode rn) { if (!rn) { return; } lockClimbFromRope = true; perviousNode = rn.perviuosNode; nextNode = rn.nextNode; ropeTransform = rn.transform; relativeJoint2D.connectedBody = rn.GetComponent <Rigidbody2D>(); zigzagHandMoveOnRope = !zigzagHandMoveOnRope; Invoke("unlockClimbFromRope", 0.4f); }
public void AttachRope(Vector2 position, NinjaController newBody, Rigidbody2D body = null) { ropeNodes = new List <RopeNode>(); ropeNodeDistance = ropeNodePrefab.GetComponent <DistanceJoint2D>().distance; attachedBody = newBody; var current = (Vector2)transform.position; var direction = (position - current).normalized; float distance = Vector2.Distance(position, current); int numberRequired = (int)((distance * 0.75f) / ropeNodeDistance); for (int i = 0; i < numberRequired; i++) { var ropeNode = Instantiate(ropeNodePrefab, transform.position, Quaternion.identity) as RopeNode; ropeNode.name = "RopeNode" + i; ropeNode.RopeController = this; ropeNode.transform.SetParent(transform); ropeNodes.Add(ropeNode); } attachedBody.AnchoredJoint2D.connectedBody = ropeNodes[0].Rigidbody2D; for (int i = 0; i < ropeNodes.Count - 1; i++) { ropeNodes[i].AnchoredJoint2D.connectedBody = ropeNodes[i + 1].Rigidbody2D; ropeNodes[i].transform.position = current + direction * (distance * (i / (float)ropeNodes.Count)); } var last = ropeNodes[ropeNodes.Count - 1]; last.transform.position = position; if (body == null) { last.AnchoredJoint2D.connectedAnchor = position; } else { last.AnchoredJoint2D.connectedBody = body; last.AnchoredJoint2D.connectedAnchor = body.transform.InverseTransformPoint(position); } last.enabled = true; isAttached = true; }
void Update() { if (sticked) { float moveX = Input.GetAxis("Horizontal"); float moveRawX = Input.GetAxisRaw("Horizontal"); if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) { if (moveRawX != 0) { animator.SetBool("ClimbRope", false); boyTransform.parent = null; if (moveRawX > 0) { rigidBody.velocity += new Vector2(15, 15); if (humanController.flipFacing) { humanController.flipFace(); } } else { rigidBody.velocity += new Vector2(-15, 15); humanController.flipFace(); } rigidBody.isKinematic = false; connectedRopeNode.GetComponent <Rigidbody2D>().mass -= rigidBody.mass; sticked = false; if (lastRopeSystem) { lastRopeSystem.enable = false; Invoke("reEnableLastRope", 1f); } return; } else { climbUp(); } } else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) { climbDown(); } if (moveX != 0) { forceToRope(moveX); } Vector3 offset = transform.position - boyTransform.position; Vector3 charFinalPos = connectedRopeNode.transform.position - offset; Vector2 v = Vector2.Lerp(boyTransform.position, charFinalPos, Time.deltaTime * 25); boyTransform.rotation = Quaternion.RotateTowards(boyTransform.rotation , connectedRopeNode.transform.rotation, Time.deltaTime * 25); if (Vector2.Distance(v, boyTransform.position) > 0.005f) { boyTransform.position = v; } animator.SetFloat("VelocityX", connectedRopeNode.GetComponent <Rigidbody2D>().velocity.x); } }