Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can't get my StarField to work (Particle Effect)

Discussion in 'Scripting' started by johnniks, Nov 8, 2015.

  1. johnniks

    johnniks

    Joined:
    Oct 2, 2013
    Posts:
    26
    Hey, so I've been messing around for a bit on this script, but I can't get it to work, no matter what I try. I thought my issue was that I'm doing something wrong in Unity but after a bit of testing with other Particle Effects; I don't think that's the issue... so if someone could help me with figuring out why my script doesn't work, I would really appreciate it :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class StarField : MonoBehaviour {
    5.  
    6.     private Transform thisTransform;
    7.     private ParticleSystem.Particle[] points;
    8.  
    9.     public int starsMax = 100; //this represent how many stars will show up in our world
    10.     public float starSize = 1f;
    11.     public float starDistance = 10f;
    12.     public float starClipDistance = 1f; //?
    13.  
    14.     //do not touch internal math stuff
    15.     private float starDistanceSqr;
    16.     private float starClipDistanceSqr;
    17.  
    18.     void Start () {
    19.         thisTransform = transform; //caching the transform for this object up front
    20.         starDistanceSqr = starDistance * starDistance; //this was missing from the video
    21.         starClipDistanceSqr = starClipDistance * starClipDistance;
    22.     }
    23.  
    24.     private void CreateStars() {
    25.         points = new ParticleSystem.Particle[starsMax];
    26.      
    27.         for (int i = 0; i < starsMax; i++) {
    28.             points[i].position = Random.insideUnitSphere * starDistance + thisTransform.position; //sets random points within a sphere around the position of the player
    29.             points[i].color = new Color(1,1,1, 1); //red, green, blue, transparent/alpha
    30.             points[i].size = starSize; //is used to tell the size of the stars
    31.         }
    32.     }
    33.  
    34.     void Update () {
    35.      
    36.         if (points == null) {
    37.             CreateStars();
    38.         }
    39.      
    40.         for (int i = 0; i < starsMax; i++) { //the i++ is very important or else it would be permanently stuck
    41.          
    42.             if ((points[i].position - thisTransform.position).sqrMagnitude > starDistanceSqr) {
    43.                 points[i].position = Random.insideUnitSphere.normalized * starDistance + thisTransform.position;
    44.             }
    45.          
    46.             if ((points[i].position - thisTransform.position).sqrMagnitude <= starClipDistanceSqr) {
    47.                 float percent = (points[i].position - thisTransform.position).sqrMagnitude / starClipDistanceSqr;
    48.              
    49.                 points[i].color = new Color(1,1,1, percent);
    50.                 points[i].size = percent * starSize;
    51.             }
    52.             GetComponent<ParticleSystem>().SetParticles (points, points.Length);
    53.         }
    54.     }
    55.  
    56. }
     
  2. johnniks

    johnniks

    Joined:
    Oct 2, 2013
    Posts:
    26
    Not sure why, but now it works all of sudden... and I haven't changed anything >_>
     
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    what should it do and what isn't it doing?
    What shape does your particle system has,... make sure the particle system is setup so it can do what you ask.
    for example if you want them to do Random.insideUnitSphere your shape should be set to sphere, i would prefer using Random.onUnitSphere.
     
  4. johnniks

    johnniks

    Joined:
    Oct 2, 2013
    Posts:
    26
    Well, it's just supposed to look like stars when you fly around in a spaceship. So you get the notion that you're flying around (stars passing by in the distance) rather than standing completely still in the endless void of space.
    The shape is a sphere surrounding the player/spaceship.

    EDIT: but it looks really dumb if you fly up/down or left/right... it only looks decent when you go forward and backward.