I’m trying to constrain a crosshair to the screen position of the mouse. I believe it’s called the viewport in Unity, so the Viewport position of the mouse, technically. Either way, I’ve followed multiple examples that I’ve found all over the internet, and tried many solutions.
I have instantiated the crosshair prefab as a GameObject using a scene initialization script, and yes, I have double checked that the crosshair prefab contains the crosshair, mouse positioning script that I’ve made. Everything seems to be in place, yet, when I start a scene, the crosshair is spawned like normal, yet it doesn’t move at all. Here’s where I do all of the positioning.
void Update()
{
Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y); //Get mouse position
crosshairPosX = this.transform.position.x; //Get x and y position of crosshair
crosshairPosY = this.transform.position.y; //
crosshairPosX = mousePosition.x; //Set x and y position to mouse position
crosshairPosY = mousePosition.y; //
//Camera.main.ScreenToWorldPoint(Input.mousePosition);
//crosshairPosX = mousePos.x;
//crosshairPosY = mousePos.y;
}
The commented code below the actual positioning, which contains the “ScreenToWorldPoint” statement, is one other attempt that I haven’t removed yet. It was one of the solutions I tried.