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. Dismiss Notice

day night cycle

Discussion in 'Scripting' started by kajvanschalkwijk, Feb 10, 2021.

  1. kajvanschalkwijk

    kajvanschalkwijk

    Joined:
    May 19, 2020
    Posts:
    4
    I want to create a day-night cycle but when it is at the sun will move slower than when it is night how to do this in c#.
     
  2. kajvanschalkwijk

    kajvanschalkwijk

    Joined:
    May 19, 2020
    Posts:
    4
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Sun : MonoBehaviour
    {
    public float DaySpeed = 2f;
    public float NightSpeed = 15f;
    // Update is called once per frame
    void Update()
    {
    float speed;

    if (transform.position.y >= -1.4)
    {
    speed = DaySpeed;
    }

    else
    {
    speed = NightSpeed;
    }

    transform.RotateAround(Vector3.zero, Vector3.right, speed * Time.deltaTime);
    transform.LookAt(Vector3.zero);
    }
    }

    this is the script
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You've stated what you want the script to do. You've posted a script which at first glance appears to do what you want the script to do. What you haven't done is actually stated a question.
     
  5. JTAGames

    JTAGames

    Joined:
    Mar 7, 2019
    Posts:
    13


    This is our tutorial on creating a day and night system. Just angle the directional light so it's nighttime longer. Which will actually simulate what it's like in real life, since earth and any planet that will have seasons spins on an a tilted axis.
     
    Last edited: Feb 9, 2022
    tonyomendoza likes this.