Hello, I’m making a RTS game, and i wanted to do something kind of like ‘Age of empires’
Basically I want to be able to move my camera based on where my mouse is on the screen. If that makes any sense… My code that i tried is:
var mousePos = Input.mousePosition;
mousePos.x -= Screen.width / 2;
mousePos.y -= Screen.height / 2;
gameObject.transform.Translate(mousePos.x / 100, 0, mousePos.y / 100); //gameObject is the camera.
I keep getting a error:
Get_Mouseposition can only be called from the main thread. Constructers and Field Initializers will be executed from the loading thread when loading a scene.
Thanks in advance, if You can help
Hey, I found this tutorial, maybe it’ll help.
If I remember my AoE, the screen would scroll when you moved your mouse to the edge. Your code… if it worked would just keep scrolling to the top left constantly. You want something like this:
//This code assumes that top left of the screen is (0,0)
//and bottom right is (Screen.width, Screen.height).
//Also this might have compiler errors, as I usually code in
//c#, so I appologize if that's true.
var scrollSpeed = 20;
mousePos = Input.mousePosition; //We need to get the new position every frame
//if mouse is 50 pixels and less from the left side of the
//screen, we move the camera in that direction at scrollSpeed
if(mousePos.x < 50)
gameObject.transform.Translate(-scrollSpeed, 0, 0);
//if 50px or less from the right side, move right at scrollSpeeed
if(mousePos.x > Screen.width - 50)
gameObject.transform.Translate(scrollSpeed, 0, 0);
if(mousePos.y < 50)
gameObject.transform.Translate(0, 0, scrollSpeed);
if(mousePos.y > Screen.height - 50)
gameObject.transform.Translate(0, 0, -scrollSpeed);