I am making a star system and I am using a particle system, the only thing I need to do is make the particle system not show up during the day, how I accomplish this does not really matter but everything I have googled is from 6+ years ago and is not working correctly. No matter what I seem to do I get errors and it is very frustrating any help would be appreciated.
using UnityEngine;
using System.Collections;
public class AtmosphericConditioning : MonoBehaviour
{
// The directional light which we manipulate as our sun.
public Light sun;
// The particle system which we manipulate as our stars.
public ParticleSystem starSystem01;
public ParticleSystem starSystem02;
// The number of real-world seconds in one full game day.
// Set this to 86400 for a 24-hour realtime day.
public float secondsInFullDay = 3600f;
// The value we use to calculate the current time of day.
// Goes from 0 (midnight) through 0.25 (sunrise), 0.5 (midday), 0.75 (sunset) to 1 (midnight).
// We define ourself what value the sunrise sunrise should be etc., but I thought these
// values fit well. And now much of the script are hardcoded to these values.
[Range(0, 1)]
public float currentTimeOfDay = 0.5f;
// A multiplier other scripts can use to speed up and slow down the passing of time.
[HideInInspector]
public float timeMultiplier = 1f;
// Get the initial intensity of the sun so we remember it.
float sunInitialIntensity;
//camera to follow
public Camera cameraToFollow;
//Sets the current time
[HideInInspector]
float currentTime = 0.0f;
[HideInInspector]
public string timeString = "00:00 AM";
void Start() {
sunInitialIntensity = sun.intensity;
//Sets the current time to the starting time.
currentTime = currentTimeOfDay;
}
void Update() {
//increment time
currentTime += Time.deltaTime * timeMultiplier;
;
//reset time
if (currentTime >= 24.0f)
{
currentTime %= 24.0f;
}
// Updates the sun's rotation and intensity according to the current time of day.
UpdateSun();
// This makes currentTimeOfDay go from 0 to 1 in the number of seconds we've specified.
currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;
// If currentTimeOfDay is 1 (midnight) set it to 0 again so we start a new day.
if (currentTimeOfDay >= 1)
{
currentTimeOfDay = 0;
}
//Controls the nested camera system.
ControlCamera();
}
void UpdateSun()
{
// Rotate the sun 360 degrees around the x-axis according to the current time of day.
// We subtract 90 degrees from this to make the sun rise at 0.25 instead of 0.
// I just found that easier to work with.
// The y-axis determines where on the horizon the sun will rise and set.
// The z-axis does nothing.
sun.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 90, 90, 0);
// The following determines the sun's intensity according to current time of day.
// You'll notice I have hardcoded a bunch of values here. They were just the values
// I felt worked best. This can obviously be made to be user configurable.
// Also with some more clever code you can have different lengths for the day and
// night as well.
// The sun is full intensity during the day.
float intensityMultiplier = 1;
starSystem01.Clear();
starSystem02.Clear();
// Set intensity to 0 during the night night.
if (currentTimeOfDay <= 0.23f || currentTimeOfDay >= 0.75f)
{
intensityMultiplier = 0;
starSystem01.Play();
starSystem02.Play();
}
// Fade in the sun when it rises.
else if (currentTimeOfDay <= 0.25f)
{
// 0.02 is the amount of time between sunrise and the time we start fading out
// the intensity (0.25 - 0.23). By dividing 1 by that value we we get get 50.
// This tells us that we have to fade in the intensity 50 times faster than the
// time is passing to be able to go from 0 to 1 intensity in the same amount of
// time as the currentTimeOfDay variable goes from 0.23 to 0.25. That way we get
// a perfect fade.
intensityMultiplier = Mathf.Clamp01((currentTimeOfDay - 0.23f) * (1 / 0.02f));
}
// And fade it out when it sets.
else if (currentTimeOfDay >= 0.73f)
{
intensityMultiplier = Mathf.Clamp01(1 - ((currentTimeOfDay - 0.73f) * (1 / 0.02f)));
}
// Multiply the intensity of the sun according to the time of day.
sun.intensity = sunInitialIntensity * intensityMultiplier;
}
void ControlCamera() {
//Get camera
if (!cameraToFollow)
{
cameraToFollow = Camera.main;
return;
}
//set position to the camera
if (cameraToFollow)
{
transform.position = cameraToFollow.transform.position;
}
}
void CalculateTime() {
//Is it am of pm?
string AMPM = "";
float minutes = ((currentTime) - (Mathf.Floor(currentTime))) * 60.0f;
if (currentTime <= 12.0f)
{
AMPM = "AM";
}
else {
AMPM = "PM";
}
//Make the final string
timeString = Mathf.Floor(currentTime).ToString() + " : " + minutes.ToString("F0") + " " + AMPM;
}
}
The errors occur right inside of the if statement to disable the sun at night, on lines 94 anad 95 and 101 and 102.
Please consider posting the actual errors. Each one should have a specific line number in it, and give you clues to what the problem might be. If you post only the code, none of us are going to go over it byte-by-byte looking for possible issues: that’s what the compiler is for.
I found out one problem but now I am stumped, I got rid of the compiler errors finally and posted my code. Now stars do not appear at all, or so I thought. on closer inspection I see a flash every now and then, which gave me a theory, I think it is because it is running the play every single frame, How can i fix this so that they only show at night which has to be declared every frame, while only having the stars play once? Is my theory even correct?
[quote=Kurt Dekker, post: 2649557, member: 225647]
Please consider posting the actual errors. Each one should have a specific line number in it, and give you clues to what the problem might be. If you post only the code, none of us are going to go over it byte-by-byte looking for possible issues: that’s what the compiler is for.
[/quote]Sorry Kurt, I have updated the post the code is on to the code I am currently using, I messed around with the code a lot since I posted this. I am one step closer I think, the lines that deal with the stars are below the script, 94-95 and 101-102.
You clear the particle system every frame regardless of day or night, you will want to stop this behaviour at night, that should sort your flashing on and off issue
So I was right, but if i fix it the only way I know how, by putting an if statement similar to the one it is in now, only in start and not update, it will only run once and that is no good either, so how do i fix that?
Edit: Oh duh, just delete the play functions, I do not know how I did not think of that earlier, I will have to play around with this some more because that did not work, though I watched the sky closely and I no longer see any brief flashes, so I will have to play around with the particle system some more, thank you
In updateSun call the clear funtions if its not night time, you have got the ifs just below it, now you just need to place the code within the parts when its not nighttime. Also add an else at the end and clear it.
Yeah I am getting it, I deleted the lines for clear and play and put an else statement, do I just put
else
{
starSystem01.Clear();
starSystem02.Clear();
}
Because it gives an error after the last bracket and says it was expecting a bracket, I am 100% sure all my brackets are closed properly, so I just put an else if statement and that worked, now my stars appear at night and they are solid, only they do not appear in the sky more than one at a time and very very slowly and I need a way to make each one shine every so often, but for now it is good enough. thanks for the help.