Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Rope : MonoBehaviour {
- public Rigidbody2D hook;
- public GameObject linkPrefab;
- public Weight weight;
- public RopeCutter cutter;
- public int links = 7;
- public bool canBeTriggered = false; // Used to check if this rope hook can create rope upon being tapped
- public bool tetherOnStart = false; // If true, automatically create rope between tether and ball upon start
- void Start () {
- if (tetherOnStart)
- {
- GenerateRope();
- }
- }
- void GenerateRope ()
- {
- Rigidbody2D previousRB = hook;
- for (int i = 0; i < links; i++) // Make some links
- {
- GameObject link = Instantiate(linkPrefab, transform);
- HingeJoint2D joint = link.GetComponent<HingeJoint2D>();
- joint.connectedBody = previousRB;
- if (i < links - 1) // If this is the last link in the chain, stop here and connect weight
- {
- previousRB = link.GetComponent<Rigidbody2D>();
- } else
- {
- weight.ConnectRopeEnd(link.GetComponent<Rigidbody2D>());
- }
- }
- }
- private void OnMouseDown()
- {
- if (canBeTriggered) // If this rope can be activated and was pressed, draw rope to weight
- {
- cutter.ResetCooldown();
- GenerateRope();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment