C# - Day / Night Cycle - Sun Moving @ 4x Speed?

Hello. This is my first time posting here on UnityAnswers. I’ve been testing out Unity Indie for a while and I’m making a script to move a light source, with the script attached, along the circumference of a circle on the X/Y plane. I eventually plan to pair it with a shader I have written that blends the skybox between a night and day variant via a float variable for a complete Day/Night Cycle effect.

However, while I have worked with both C# and shaders before, my trigonometry is HORRIBLE and for some reason my light source is moving at 4x the speed it should be. After reviewing trig online for several hours and searching for people who have had similar problems (with few results), I’ve decided to finally make a post here. Below is my script:

using UnityEngine;
using System;
using System.Collections;

public class MySun : MonoBehaviour {

	public float sunRadius		= 10.0f;
	public Vector3 sunPosition	= new Vector3();

	private Vector3 sunOrigin = new Vector3();
	private int syncTick = 0;
	private int sunAngle = 0;

	void Start () {
		sunOrigin = transform.localPosition;
	}

	...

	void Update () {
		if (syncTick >= timeScale) {
			if (sunAngle >= 360) sunAngle = 0;
			syncTick = 0;
			sunAngle++;
		}
		updateSunPosition();
		transform.LookAt(sunOrigin);
		syncTick++;
	}

	...

	private void updateSunPosition(){
		sunPosition.x = sunOrigin.x + (sunRadius * (float)Math.Cos(Mathf.Deg2Rad * sunAngle));
		sunPosition.y = sunOrigin.y + (sunRadius * (float)Math.Sin(Mathf.Deg2Rad * sunAngle));
		transform.localPosition = sunPosition;
	}
}

I’ve trimmed everything that doesn’t deal with positioning, as I’m sure that those bits are not causing the problem. A million thanks to whoever can help! I’ve been stuck on figuring this out for almost a day!

Hi there! Try to do like this:

    void Update () {
       if (syncTick >= timeScale) {
         if (sunAngle >= 360) sunAngle = 0;
         syncTick = 0;
         sunAngle += 20;
       }
       updateSunPosition();
       transform.LookAt(sunOrigin);
       syncTick += 20;
    }

It should work! Try!

This Should work then:

void Update () {
   if (syncTick >= timeScale) {
     if (sunAngle >= 360) sunAngle = 0;
     syncTick = 0;
     sunAngle++;

     yield return new WaitForSeconds(5.0F);
   }
   updateSunPosition();
   transform.LookAt(sunOrigin);
   syncTick++;
}

I Hope this Worked :slight_smile: - Yohan

OK! I AM GOING TO SAVE YOU!
Press Thumbs Up! :smiley:
And Hit That Correct Button!

HERE’S THE ANSWER!

using UnityEngine;
using System;
using System.Collections;

public class MySun : MonoBehaviour {
	public float sunRadius     = 10.0f;
    public Vector3 sunPosition = new Vector3();

    private Vector3 sunOrigin = new Vector3();
    private int syncTick = 0;
    private int sunAngle = 0;

    void Start () {
       sunOrigin = transform.localPosition;
	   Do();
    }

   IEnumerator Do() {
		if (syncTick >= timeScale) {
		if (sunAngle >= 360) sunAngle = 0;
		syncTick = 0;
		sunAngle++;

		yield return new WaitForSeconds(5.0F);
		Do();
   }
	updateSunPosition();
	transform.LookAt(sunOrigin);
	syncTick++;
   }
   
   private void updateSunPosition(){
       sunPosition.x = sunOrigin.x + (sunRadius * (float)Math.Cos(Mathf.Deg2Rad * sunAngle));
       sunPosition.y = sunOrigin.y + (sunRadius * (float)Math.Sin(Mathf.Deg2Rad * sunAngle));
       transform.localPosition = sunPosition;
    }
}