- Home /
Question by
XTRA_THICC · Apr 06, 2020 at 10:18 AM ·
c#mousevisual studiomouse positionscreen resolution
Neither CursorLockMode.Confined or SetResolution work
I am making a strategy game and I want my camera to scroll when I position my mouse on the edge of the screen. But it's annoying when the mouse can move outside the game window, I have to be pixel perfect for my camera to move.
So I wanted to confine my mouse to game window, but it didn't work. Then I tried making the game window fullscreen, that didn't work either.
Here's the code:
private float _scrollSpeed;
private void Start()
{
_scrollSpeed = 10.5f;
// Doesn't work.
Screen.SetResolution(1920, 1080, true);
}
private void Update()
{
ScrollBehaviour();
if (Input.GetKey(KeyCode.Space))
Cursor.lockState = CursorLockMode.None;
}
private void ScrollBehaviour()
{
Vector3 direction;
if (Input.mousePosition.x == 0) // I kaire
{
direction = new Vector3(-1, 0, 0) * _scrollSpeed * Time.deltaTime;
gameObject.transform.parent.Translate(direction, Space.Self);
}
if (Input.mousePosition.y == 0) // I apacia
{
direction = new Vector3(0, 0, -1) * _scrollSpeed * Time.deltaTime;
gameObject.transform.parent.Translate(direction, Space.Self);
}
if (Input.mousePosition.x == Screen.width-1) // I desine
{
direction = new Vector3(1, 0, 0) * _scrollSpeed * Time.deltaTime;
gameObject.transform.parent.Translate(direction, Space.Self);
}
if (Input.mousePosition.y == Screen.height) // I virsu
{
direction = new Vector3(0, 0, 1) * _scrollSpeed * Time.deltaTime;
gameObject.transform.parent.Translate(direction, Space.Self);
}
}
void OnGUI()
{
// This works.
if (GUI.Button(new Rect(0, 0, 100, 50), "Lock Cursor"))
{
Cursor.lockState = CursorLockMode.Locked;
}
// But this doesn't work.
if (GUI.Button(new Rect(125, 0, 100, 50), "Confine Cursor"))
{
Cursor.lockState = CursorLockMode.Confined;
}
}
I am not sure if Screen.SetResolution is the right fucntion for making game screen fullscreen, but it doesn't do anything. All help is appreciated.
Comment
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to move game object towards mouse on x and z only? 1 Answer
Multiple Cars not working 1 Answer