Making a bed

I am making a script that has to be attached to an object that detects if the player has clicked on it and it cannot be from too far away, I have no idea where to begin and need help. When you click on it it should increase the time of day from another script, the problem I am having here is that the time of day is a number 0-1, .25 being sunrise and .75 being sunset if I just set it to 0 it will screw up and restart the day but since when it hits 1 it gets put at 0 I cannot just add say .1 to it until it gets to 25. I am sure that part is not as hard as the mouse click on game object but it will still be a challenge for me.

my time of day script that the bed will need to access to change the time of day.

using UnityEngine;
using System.Collections;

public class AtmosphericConditioning : MonoBehaviour
{

    // The directional light which we manipulate as our sun and moon.
    [Header("Day Night System")]
    public Light sun;

    // The particle system which we manipulate as our stars.
    public ParticleSystem starSystem;

    //Variables to keep track of the default fog.
    [HideInInspector]
    public bool defaultFog;
    [HideInInspector]
    public Color defaultFogColor;
    [HideInInspector]
    public float defaultFogDensity;

    [Header("Fog system System")]
    public Color fogColor = Color.white;
    public float fogDensity = 0.01f;

    // The number of real-world seconds in one full game day.
    // Set this to 86400 for a 24-hour realtime day.
    [Header("Day Length System")]
    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.
    [HideInInspector]
    [Range(0, 1)]
    public static 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
    [Header("Nested Camera System")]
    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;

        //gets the default fog values.
        defaultFog = RenderSettings.fog;
        defaultFogColor = RenderSettings.fogColor;
        defaultFogDensity = RenderSettings.fogDensity;

    }

    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;

        // Set intensity to 0 during the night and control our stars.
        if (currentTimeOfDay <= 0.23f || currentTimeOfDay >= 0.75f) {
            intensityMultiplier = 0.01f;
            starSystem.Play();
        }
        else if (currentTimeOfDay > 0.25f || currentTimeOfDay < 0.75f) {
            starSystem.Clear();
        }

        // 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;

    }
}

for the mouse click you should look up raycast. A raycast shoots an invisable line. if the line hits an abject you can do what ever you’d like. or if you are clicking on a GUI button, then just use the onTrigger event onClick(). That should be enough info to get you started.

The part about 0 makes it start all over. it looks like the float currentTimeOfDay is what your talking about. rom the script if the time = 1, (midnight), then yes if will set the float to = 0. this looks correct to me.

Here you go, import this scene and see if is what you’re wanting.

2675573–188916–SunSystem.unitypackage (19.2 KB)

Thank you for that test scene I am sure I can use that concept in my script, My problem with that is that I was worried I would either break the day and night by going over 1 or that if I just set it to morning noon or evening that it would reverse time which would also be no good. but that example should help me a lot.

With raycasts from what I have googled and seen they need to be attached to the camera, I need something I can attach to the game object, is it possible to use raycast with an object rather than attaching it to a camera? I will read more about it regardless, I am just curious to see if I will need to look into another method since I need it on the object.

Hi yourking77,
Glad you liked the sample scene I sent you.
With Raycast. They don’t have to be attached to a camera. for exp. a skydiver jumps from an air plain. with a raycast attached to the skyjumper object. the raycast is setup to shoot towards the ground. with a ray distanced of 50. So when the jumpers reaches a distance of 50 from the ground the raycast hit will be true, then you tell the jumper to pull his parachute and slow he safely lands on the ground.

A lot of games use the raycast with a camera to do mouse clicks. this doesn’t mean the script has to be on the camera.
A raycast needs 3 parameters. a start position, a direction, a distance. so with the mouse click, the camera is the start position.

do you need me to setup the scene with a raycast that clicks on an object to set the time?

so the raycast is set up to the camera which basically shoots an invisible beam, then on say chests, doors, beds, basically all interactive objects I can attach a script to those objects and make them “work” when the raycast is pointed at them? I think that helps a lot I was just about to look into raycasts actually.

If you could create an example project that would be great if it is not too much trouble. That last one really helped, I can still barely understand it XD but that is how it usually goes when I get help from people with alot more skill than I do, but I will disect it and recreate it and it not only gives me a good guid eon what to do but will also help me learn some more about the code.

ok, i’ll start working on it. check back here in an hour or two.

it was to setup. here you go.
So you just click the box and it will change it’s scale each time you click the object.

2675807–188939–SunSystem2.unitypackage (20 KB)

Thank you so much, for now I have enough to try and build a few of my systems up, I will come back here if I have any problems but for now I think I can manage. I know everyone else says to read and look at youtube videos, but I prefer looking at what other people have done and how other things work to figure it out for myself having something to mess with keeps me interested enough to actually learn it, so I really appreciate this.