Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Starfield : MonoBehaviour
  6. {
  7.  
  8. //This is the object with this script attached (camera)
  9. private Transform thisTransform;
  10. //The particles (stars)
  11. private ParticleSystem.Particle[] points;
  12. private float starDistanceSqr;
  13. private float starClipDistanceSqr;
  14.  
  15. [Tooltip("The color of the stars")]
  16. public Color starColor;
  17. [Tooltip("The max amout of stars occuring in the scene")]
  18. public int starsMax;
  19. [Tooltip("The size of the stars")]
  20. public float starSize;
  21. [Tooltip("The distance for how far out the stars can appear")]
  22. public float starDistance;
  23. [Tooltip("The distance when the stars will disappear near the player")]
  24. public float starClipDistance;
  25.  
  26. void Start()
  27. {
  28. thisTransform = GetComponent<Transform>();
  29. starDistanceSqr = starDistance * starDistance;
  30. starClipDistanceSqr = starClipDistance * starClipDistance;
  31. }
  32.  
  33. private void CreateStars()
  34. {
  35. points = new ParticleSystem.Particle[starsMax];
  36.  
  37. for (int i = 0; i < starsMax; i++)
  38. {
  39. points[i].position = Random.insideUnitSphere * starDistance + thisTransform.position;
  40. points[i].startColor = new Color(starColor.r, starColor.g, starColor.b, starColor.a);
  41. points[i].startSize = starSize;
  42. }
  43. }
  44.  
  45. void Update()
  46. {
  47. //If there is no points(stars) then it will run the create stars script
  48. if (points == null) CreateStars();
  49.  
  50. for (int i = 0; i < starsMax; i++)
  51. {
  52. if ((points[i].position - thisTransform.position).sqrMagnitude > starDistanceSqr)
  53. {
  54. points[i].position = Random.insideUnitSphere.normalized * starDistance + thisTransform.position;
  55. }
  56.  
  57. if ((points[i].position - thisTransform.position).sqrMagnitude <= starClipDistanceSqr)
  58. {
  59. float percentage = (points[i].position - thisTransform.position).sqrMagnitude / starClipDistanceSqr;
  60. points[i].startColor = new Color(1, 1, 1, percentage);
  61. points[i].startSize = percentage * starSize;
  62. }
  63. }
  64. GetComponent<ParticleSystem>().SetParticles(points, points.Length);
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement