How can I make the moon following the player (2D)


I am currently working on a 2D Game and I want to create the effect when the moon shines through a bunch of trees and slowly follows the player around. I am not sure if it should slowly enter the view and leaving after a while or constantly being there, but giving the impression of losing sight of it if the player walks around long enough in one direction. The moon should be a sprite or a plane.

How can I achieve this as simple as possible? I am pretty much a rookie when it comes to scripting, since I am a Game Art student that just has been introduced recently to scripting, so please try to be as simple as possible :slight_smile:

Thank you very much

This is assuming you move the camera to follow the player on the screen.
Set the position of the moon object to be some arbitrary position (MoonOffset) and then move it on the x-axis almost the same amount as you move the camera (times 0.9 in the example).

public GameObject Moon;
public Vector3 MoonOffset; // use to position the moon where you want to begin with
void Update() {
	    var pos = MoonOffset;
	    pos.x += Camera.main.transform.position.x * 0.9f;
	    Camera.main.transform.position = pos;

Thank you very much! :slight_smile: