# Apply Angle on Day/Night Cycle

Hi, so I’m pretty new to Unity and game design in general. I’ve successfully gotten a sun and moon to orbit around my map using this script:

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SunMoon : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
transform.RotateAround(Vector3.zero,Vector3.back,5f*Time.deltaTime);
transform.LookAt(Vector3.zero);
}
}
``````

But I would like to apply an angle to the orbit like in this diagram:

How would I do that?

Thanks!

I’m not sure I understand the diagram, but if you are just trying to tilt the plane of the orbit, all you need to do is change the second argument of RotateAround from “Vector3.back” to the unit vector that is perpendicular to the desired plane of rotation.

For instance, if you used `(Vector3.back + Vector3.up).normalized` that should tilt the plane of rotation by 45 degrees.

1 Like

Instead of rotating by deltaTime, I’d have a ‘timeofday’ value (may you just use the built in Time.time).

Next, you’re going to need an amount of tilt which you’ll combine to the sun/moon rotation.

And you’ll need to get a rotation based on the time of day.

Something like this:

``````    public float TimeOfDay = 0f;
public float DayScale = 1f;
public float Tilt = 30f; //tilt in degrees

void Update()
{
this.TimeOfDay += Time.deltaTime * this.DayScale;

const float SECS_IN_DAY = 60f * 60f * 24f;
var tilt = Quaternion.AngleAxis(this.Tilt, Vector3.forward);
var rot = Quaternion.AngleAxis(360f * (this.TimeOfDay % SECS_IN_DAY) / SECS_IN_DAY, Vector3.right);
this.transform.rotation = tilt * rot;
}
``````

You may need to tweak the Vector3.forward, and the Vector3.right, to whatever axises you tilt around and rotate around respectively.

There’s of course other ways to do it… I went this route to nudge you towards quats, which is how all of this works underneath (like RotateAround), and to show you how to create compound rotations.

I don’t know why but it’s trowing up this namespace error. From what I gather I need to add some kind of `using Something;` at the top but I don’t know what that something is.

That method doesn’t seem to work I ended up with a sun and moon on different orbits.

https://vimeo.com/388534939

Oh right nevermind I’m an idiot. I had to rotate the sun and moon.