Advertisement
Guest User

Rope

a guest
Jan 26th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class RopeTest : MonoBehaviour
  6. {
  7. public LineRenderer line;
  8.  
  9. public Transform StartPoint;
  10. public Transform EndPoint;
  11.  
  12. public RaycastHit hitStart;
  13. public RaycastHit hitEnd;
  14.  
  15.  
  16. public float length;
  17. public int Points = 1; // Keeps track of the total amount of points
  18. public Transform PointPositions; // This transforms position is set to the second to last point on the line.
  19. public Transform SecondEndPoint; // This transform should have the same position as the EndPoint. Its used for unwrapping the rope.
  20. private void Start()
  21. {
  22. PointPositions.position = StartPoint.position;
  23.  
  24. }
  25. public LayerMask layersToHit;
  26. private void FixedUpdate()
  27. {
  28. line.SetPosition(0, StartPoint.position);
  29. line.SetPosition(Points, EndPoint.position);
  30.  
  31. length = Vector3.Distance(PointPositions.position, EndPoint.position);
  32.  
  33.  
  34.  
  35.  
  36.  
  37. //This Raycast starts from the PointPositions transform and goes to the EndPoint transform
  38. EndPoint.LookAt(PointPositions);
  39. if(Physics.Raycast(PointPositions.position, PointPositions.forward, out hitStart, length, layersToHit))
  40. {
  41. PointPositions.position = hitStart.point;
  42.  
  43. CreatePoint();
  44.  
  45. }
  46.  
  47. // This Raycast goes from the EndPoint to the PointPositions transform to better detect if one of the raycasts hits the point.
  48. PointPositions.LookAt(line.GetPosition(Points));
  49. if (Physics.Raycast(EndPoint.position, EndPoint.forward, out hitEnd, length, layersToHit))
  50. {
  51. PointPositions.position = hitEnd.point;
  52.  
  53. CreatePoint();
  54. }
  55.  
  56. //This creates a Raycast when there are 2 or more points on the line. The raycast aims at the third to last point on the line and if it doesn't hit anything it unwraps.
  57. if (Points >= 2)
  58. {
  59. SecondEndPoint.LookAt(line.GetPosition(Points - 2));
  60.  
  61. if (Physics.Raycast(SecondEndPoint.position, SecondEndPoint.forward, out hitEnd, length, layersToHit))
  62. {
  63. Debug.Log("DontUnwrap");
  64. }
  65. else
  66. {
  67. RemovePoint();
  68. }
  69. }
  70.  
  71. }
  72.  
  73.  
  74.  
  75. public float Distance;
  76. public float MinValue = 0.05f;
  77. public void CreatePoint()
  78. {
  79. //This makes sure that there are no overlapping points in the line.
  80. Distance = Vector3.Distance(PointPositions.position, line.GetPosition(Points - 1));
  81. if(Distance > MinValue)
  82. {
  83.  
  84. Points = Points + 1;
  85. line.positionCount = line.positionCount + 1;
  86. line.SetPosition(Points - 1, PointPositions.position);
  87. line.SetPosition(Points, EndPoint.position);
  88.  
  89. }
  90. }
  91. public void RemovePoint()
  92. {
  93. Points = Points - 1;
  94. line.positionCount = line.positionCount - 1;
  95. line.SetPosition(Points, EndPoint.position);
  96. PointPositions.position = line.GetPosition(Points - 1);
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement