Camera.main.screenToWorldpoint() breaking on build, stays centered to scene, not camera,Camera.ScreenToWorld Position relative to world center on build
So i'm making a small roguelike game, and wrote a script to make an object follow the mouse for aiming. This worked fine in the editor, but as soon as tried to build and run the scene, the mouse tracking seemed to break. As soon as the camera is moved, it becomes apparent that the object following the mouse cursor is stuck to the center of the scene, still tracking the mouse, but doing so as if the camera were still in the starting position. Here's the script for the object:
public class CursorFollow : MonoBehaviour
{
void Update()
{
transform.position = Vector2.Lerp(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), 0.5f);
}
}
I made a cursor object with a crosshair sprite attached to it, but do I need to make it a child of the camera, too?
the page bugged and merged two titles from another version of this post, sorry for that
The only thing i can tell you is to try debuging transform.position, camera.main.transform.position input.mouseposition and check whats actually not updating, also avoid using Camera.main in update since is really inneficient.
Answer by tormentoarmagedoom · Jan 22, 2019 at 09:01 AM
Good day.
Are you sure Camera.main is acting correctly? Maybe you should assign it as any other object (like in inspector or finding the compoenent camera from the camera GameObjects via script) but not using camera.main.
Bye.