TheLetterM

Ball And Tether: Rope

May 14th, 2023
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Rope : MonoBehaviour {
  4.  
  5.     public Rigidbody2D hook;
  6.  
  7.     public GameObject linkPrefab;
  8.  
  9.     public Weight weight;
  10.     public RopeCutter cutter;
  11.  
  12.     public int links = 7;
  13.  
  14.     public bool canBeTriggered = false; // Used to check if this rope hook can create rope upon being tapped
  15.     public bool tetherOnStart = false; // If true, automatically create rope between tether and ball upon start
  16.     void Start () {
  17.         if (tetherOnStart)
  18.         {
  19.             GenerateRope();
  20.         }
  21.        
  22.     }
  23.  
  24.     void GenerateRope ()
  25.     {
  26.         Rigidbody2D previousRB = hook;
  27.         for (int i = 0; i < links; i++) // Make some links
  28.         {
  29.             GameObject link = Instantiate(linkPrefab, transform);
  30.             HingeJoint2D joint = link.GetComponent<HingeJoint2D>();
  31.             joint.connectedBody = previousRB;
  32.  
  33.             if (i < links - 1) // If this is the last link in the chain, stop here and connect weight
  34.             {
  35.                 previousRB = link.GetComponent<Rigidbody2D>();
  36.             } else
  37.             {
  38.                 weight.ConnectRopeEnd(link.GetComponent<Rigidbody2D>());
  39.             }
  40.  
  41.            
  42.         }
  43.     }
  44.  
  45.     private void OnMouseDown()
  46.     {
  47.         if (canBeTriggered) // If this rope can be activated and was pressed, draw rope to weight
  48.         {
  49.             cutter.ResetCooldown();
  50.             GenerateRope();
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment