Light Arount mouse Cursor

Hello Team

I’m wonder if you can point me into the right direction. Its the first time where I try outside a Course to do something simple from scratch, but stuck with following.

In my scene I would like that the cursor is the main light. Like in the movie below in the 29th second


So at the moment I’ve something below:

private Vector3 mousePosition = new Vector3(0, 0, 0);
public GameObject Flashlight;

// Use this for initialization
void Start()
{
    Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}

// Update is called once per frame
void Update()
{
    moveFlashlight();
}

void moveFlashlight()
{
    

    Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Vector2 mousePosition2 = Input.mousePosition;


    Debug.Log(mousePosition);
    Debug.Log(mousePosition2);

    Flashlight.transform.position = new Vector3(mousePosition.x, mousePosition.y, 0);

}

The Flashlight is a Gameobject where I want to Insert the Light object.

The Debug log its only to show me if the values are changing when I move the mouse.

Whats interesting is the when I start the Scene mousePosition only shows the first vaule, where mouseposition2 is changing it.

What do I need to do so the Light will be where the Cursor is.

There is probably a better solution, maybe also a built in Function in Unity?

Hi there, if you’re working in 2D a quick way to fix this is to change you’re camera mode from Perspective to Orthographic, in your Main Camera Inspector. Or if you require perspective, a crude solution is:

void moveFlashlight()
	{
		//You can replace "new Vector3(Input... with a variable for cleaner code
		Vector3 mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,10f));
		Flashlight.transform.position = new Vector3(mousePosition.x, mousePosition.y, 0f);
	}