mandag den 31. oktober 2016

Camera Control


Hello i'm Lars from the HiddenGames Programmer Team.

Recently i've been working on a Camera Control Script for the video game Corporate Quest with Thorulf,  it turned out well and is now in full use in the demo we are making. 

What is it and why did you make it?
The reason why we made this was for the feeling of dragging around the map like it was a paper. We also wanted to limit the players view, to ensure they focus on Seattle first before the other parts of the map. The camera movement was decided to be free map movement around a dedicated part of the game, where you can zoom in and out on cities.


How do you use it?
It works like this:
  • Hit the right mousebutton while moving the mouse in the opposite direction of where you want to go
  • Zoom in and out with the mouse scroller to show more or less of the map 
How does the script looks like?
The script would be too long to describe completely as it fills 100 lines, but i'll highlight the most interesting parts

Rightclick mouse and drag
The script checks for input from the right mouse button, if there's input it takes the input and inverts it. The script then checks the limit that was set in the editor to see if it can move and if it can, then the code will move the camera.

// Camera dragging movement
if (Input.GetKey(KeyCode.Mouse1)) { 

// Gets the movement in unity units and inverts it for correct controls
Vector3 input = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.main.ScreenToWorldPoint(lastMousePos)) * -1;

//Gets the screen size for limiting of movement
Vector2 camPosTopRight = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));
Vector2 camPosBottomLeft = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0));

//moves the camera if movement doesnt cross the limit
if (camPosBottomLeft.x + input.x > camLimitBottomLeft.x && camPosTopRight.x + input.x < camLimitTopRight.x) transform.Translate(new Vector3(input.x, 0));

if(camPosBottomLeft.y + input.y > camLimitBottomLeft.y && camPosTopRight.y + input.y < camLimitTopRight.y)
transform.Translate(new Vector3(0, input.y));

lastMousePos = Input.mousePosition;
}

Figure 1 - Code behind the mouse drag movement script


Camera Limit
To prevent the player from scrolling out of the map, we wanted to make limits in the editor to fit the map as it develops, you can drag two vectors in the editor to determine the camera limits.


Figure 2 - Camera Limits in the scene editor


0 kommentarer:

Send en kommentar