Global Game Jam 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.1 KiB

1 year ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [ExecuteInEditMode]
  5. public class vine : MonoBehaviour
  6. {
  7. private LineRenderer lineRenderer;
  8. [SerializeField]
  9. private GameObject position1;
  10. [SerializeField]
  11. private GameObject position2;
  12. private Vector3 lastPosition1;
  13. private Vector3 lastPosition2;
  14. private float lastDipMagnitude;
  15. private int lastNumberPoints;
  16. [SerializeField]
  17. private AnimationCurve vineCurve;
  18. [SerializeField]
  19. private float dipMagnitude = 1f;
  20. [SerializeField]
  21. private int numberOfPoints = 20;
  22. // Start is called before the first frame update
  23. void Awake()
  24. {
  25. lineRenderer = GetComponent<LineRenderer>();
  26. }
  27. void Update()
  28. {
  29. //if both positions are defined
  30. if (position1 != null && position2 != null)
  31. {
  32. //if either of the points have moved
  33. if ((position1.transform.position != lastPosition1) || (position2.transform.position != lastPosition2) || (dipMagnitude != lastDipMagnitude) || (numberOfPoints != lastNumberPoints))
  34. {
  35. Recalculate();
  36. }
  37. }
  38. }
  39. [ContextMenu("Recalculate")]
  40. void Recalculate()
  41. {
  42. lineRenderer.positionCount = numberOfPoints;
  43. //set start and end
  44. lineRenderer.SetPosition(0, position1.transform.position);
  45. lineRenderer.SetPosition(lineRenderer.positionCount-1, position2.transform.position);
  46. for (int i = 0; i<lineRenderer.positionCount; i++)
  47. {
  48. Vector3 newLerpPos = Vector3.Lerp(position1.transform.position, position2.transform.position, i / (float)(lineRenderer.positionCount - 1));
  49. Vector3 newDip = Vector3.up * dipMagnitude * (1f - vineCurve.Evaluate(i / (float)(lineRenderer.positionCount - 1)));
  50. lineRenderer.SetPosition(i, newLerpPos - newDip);
  51. }
  52. lastPosition1 = position1.transform.position;
  53. lastPosition2 = position2.transform.position;
  54. lastDipMagnitude = dipMagnitude;
  55. lastNumberPoints = numberOfPoints;
  56. }
  57. }