Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Starfield : MonoBehaviour
- {
- //This is the object with this script attached (camera)
- private Transform thisTransform;
- //The particles (stars)
- private ParticleSystem.Particle[] points;
- private float starDistanceSqr;
- private float starClipDistanceSqr;
- [Tooltip("The color of the stars")]
- public Color starColor;
- [Tooltip("The max amout of stars occuring in the scene")]
- public int starsMax;
- [Tooltip("The size of the stars")]
- public float starSize;
- [Tooltip("The distance for how far out the stars can appear")]
- public float starDistance;
- [Tooltip("The distance when the stars will disappear near the player")]
- public float starClipDistance;
- void Start()
- {
- thisTransform = GetComponent<Transform>();
- starDistanceSqr = starDistance * starDistance;
- starClipDistanceSqr = starClipDistance * starClipDistance;
- }
- private void CreateStars()
- {
- points = new ParticleSystem.Particle[starsMax];
- for (int i = 0; i < starsMax; i++)
- {
- points[i].position = Random.insideUnitSphere * starDistance + thisTransform.position;
- points[i].startColor = new Color(starColor.r, starColor.g, starColor.b, starColor.a);
- points[i].startSize = starSize;
- }
- }
- void Update()
- {
- //If there is no points(stars) then it will run the create stars script
- if (points == null) CreateStars();
- for (int i = 0; i < starsMax; i++)
- {
- if ((points[i].position - thisTransform.position).sqrMagnitude > starDistanceSqr)
- {
- points[i].position = Random.insideUnitSphere.normalized * starDistance + thisTransform.position;
- }
- if ((points[i].position - thisTransform.position).sqrMagnitude <= starClipDistanceSqr)
- {
- float percentage = (points[i].position - thisTransform.position).sqrMagnitude / starClipDistanceSqr;
- points[i].startColor = new Color(1, 1, 1, percentage);
- points[i].startSize = percentage * starSize;
- }
- }
- GetComponent<ParticleSystem>().SetParticles(points, points.Length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement