DNA.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class DNA : MonoBehaviour
  5. {
  6. private ParticleSystem PS;
  7. private ParticleSystem.MainModule _psmm;
  8. float density = 20;
  9. float curve = -7;
  10. List<Vector3> dnaPoints = new List<Vector3>();
  11. [SerializeField] float rotateSpeed = 3;
  12. System.Random random;
  13. // Use this for initialization
  14. void OnEnable()
  15. {
  16. random = new System.Random();
  17. PS = GetComponent<ParticleSystem>();
  18. _psmm = PS.main;
  19. MakeDNA(Vector3.zero, 100f);
  20. MakeLadder(100f);
  21. transform.localRotation = Quaternion.Euler(new Vector3());
  22. count = 0;
  23. }
  24. void MakeDNA(Vector3 position, float length)
  25. {
  26. float addp = 0;
  27. float height = 0;
  28. for (float p = 0; p < length * density; p++)
  29. {
  30. // create a particle with random
  31. height += 1 / density;
  32. float pX = 5;
  33. float pY = height + (random.Next(-10, 10) / 10f);
  34. float pZ = 0;
  35. Vector3 point = new Vector3();
  36. point.x = pX;
  37. Vector3 center = new Vector3();
  38. addp += 180 + (curve / density);
  39. Vector3 r = rotateAround(point, center, addp);
  40. addp %= 360;
  41. pX = r.x;
  42. pZ = r.y;
  43. dnaPoints.Add(new Vector3(pX, pY, pZ));
  44. }
  45. }
  46. void MakeLadder(float length)
  47. {
  48. float addp = 0;
  49. float height = 0;
  50. float ladderspace = 4;
  51. for (float p = 0; p <= length / ladderspace; p++)
  52. {
  53. // create a particle with random
  54. for (float i = 0; i < density * 2f; i++)
  55. {
  56. float pX = random.Next(-50, 50) / 10f;
  57. float pY = height + (random.Next(-4, 4) / 10f);
  58. float pZ = 0;
  59. Vector3 point = new Vector3();
  60. point.x = pX;
  61. Vector3 center = new Vector3();
  62. addp += 180f;
  63. Vector3 r = rotateAround(point, center, addp);
  64. addp %= 360;
  65. pX = r.x;
  66. pZ = r.y;
  67. dnaPoints.Add(new Vector3(pX, pY, pZ));
  68. }
  69. addp += curve * ladderspace;
  70. addp %= 360;
  71. height += ladderspace;
  72. }
  73. }
  74. Vector3 rotateAround(Vector3 point, Vector3 center, float angle)
  75. {
  76. angle = (angle) * (Mathf.PI / 180f); // Convert to radians
  77. float rotatedX = Mathf.Cos(angle) * (point.x - center.x) - Mathf.Sin(angle) * (point.y - center.y) + center.x;
  78. float rotatedY = Mathf.Sin(angle) * (point.x - center.x) + Mathf.Cos(angle) * (point.y - center.y) + center.y;
  79. return new Vector3(rotatedX, rotatedY);
  80. }
  81. int count = 0;
  82. // Update is called once per frame
  83. void Update()
  84. {
  85. ParticleSystem.Particle[] ps = new ParticleSystem.Particle[PS.particleCount];
  86. int pCount = PS.GetParticles(ps);
  87. for (int i = 0; i < dnaPoints.Count; i++)
  88. {
  89. if (i < pCount)
  90. {
  91. Vector3 pos = dnaPoints[i];
  92. ps[i].position = pos;
  93. }
  94. }
  95. PS.SetParticles(ps, pCount);
  96. if(!PS.isPlaying)
  97. {
  98. PS.Play();
  99. }
  100. count++;
  101. if(count >= 40)
  102. {
  103. transform.RotateAroundLocal(Vector3.up, rotateSpeed * Time.deltaTime);
  104. }
  105. }
  106. }