Guest User

Untitled

a guest
Oct 22nd, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6.  
  7. [RequireComponent(typeof(LineRenderer))]
  8. [ExecuteAlways]
  9. public class CableProceduralCurve : MonoBehaviour
  10. {
  11.  
  12. LineRenderer line;
  13.  
  14. // The Start of the cable will be the transform of the GameObject that has this component.
  15. // The Transform of the Gameobject where the End of the cable is. This needs to be assigned in the inspector.
  16. public Transform endPointTransform;
  17.  
  18. // Number of points per meter
  19. [SerializeField, Tooltip("Number of points per unit length, using the straight line from the start to the end transform.")] float pointDensity = 3;
  20.  
  21. // How much the cable will sag by.
  22. [SerializeField, Tooltip("Positive keys are applied downward on cable. Recommended use is to have keys at 0 at the start and end of the curve.")] public AnimationCurve curve;
  23.  
  24. // These are used later for calculations
  25. int pointsInLineRenderer;
  26. Vector3 vectorFromStartToEnd;
  27. Vector3 sagDirection;
  28.  
  29.  
  30.  
  31. void Start ()
  32. {
  33. line = GetComponent<LineRenderer>();
  34.  
  35. if (!endPointTransform)
  36. {
  37. Debug.LogError("No Endpoint Transform assigned to Cable_Procedural component attached to " + gameObject.name);
  38. return;
  39. }
  40.  
  41. // Get direction Vector.
  42. vectorFromStartToEnd = endPointTransform.position - transform.position;
  43. // Setting the Start object to look at the end will be used for making the wind be perpendicular to the cable later.
  44. transform.forward = vectorFromStartToEnd.normalized;
  45. // Get number of points in the cable using the distance from the start to end, and the point density
  46. pointsInLineRenderer = Mathf.FloorToInt(pointDensity * vectorFromStartToEnd.magnitude);
  47. // Set number of points in line renderer
  48. line.positionCount = pointsInLineRenderer;
  49.  
  50. // The Direction of SAG is the direction of gravity
  51. sagDirection = Physics.gravity.normalized;
  52.  
  53. //Draw();
  54. }
  55.  
  56.  
  57.  
  58.  
  59. void Draw()
  60. {
  61. Debug.Log("Printing");
  62. // What point is being calculated
  63. int i = 0;
  64.  
  65. while(i < pointsInLineRenderer)
  66. {
  67. // This is the fraction of where we are in the cable and it accounts for arrays starting at zero.
  68. float pointForCalcs = (float)i / (pointsInLineRenderer - 1);
  69.  
  70. // Calculate the position of the current point i
  71. Vector3 pointPosition = vectorFromStartToEnd * pointForCalcs;
  72. // Calculate the sag vector for the current point i
  73. Vector3 sagAtPoint = sagDirection * - curve.Evaluate(pointForCalcs);
  74. // Calculate the sway vector for the current point i
  75.  
  76. // Calculate the postion with Sag.
  77. Vector3 currentPointsPosition =
  78. transform.position +
  79. pointPosition +
  80. sagAtPoint;
  81.  
  82.  
  83. // Set point
  84. line.SetPosition(i, currentPointsPosition);
  85. i++;
  86. }
  87. }
  88.  
  89. private void Update()
  90. {
  91. Draw();
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment