I have a 2D space shooter game. I looked up some questions from unity community and came across this solution. Below is the code for my player's movement:
//horizontal <- || -horizontal ->
//vertical down || -vertical up
float h = -Input.GetAxis ("Horizontal")*speed;
float v = -Input.GetAxis ("Vertical")*speed;
// do not let the ship get out of the camera
Vector3 viewPosition = Camera.main.WorldToViewportPoint(transform.position);
viewPosition.x = Mathf.Clamp01(viewPosition.x);
viewPosition.y = Mathf.Clamp01(viewPosition.y);
transform.position = Camera.main.ViewportToWorldPoint(viewPosition);
transform.Translate (h, v, 0);
I understand these functions as 0,0 being the left bottom of the camera and 1,1 being the right top. Therefore clamp01()
is used. But with this code, half of my ship gets out of the screen, which is not what I want. How am I supposed to manipulate the clamp01 function so that the entire ship of the player stays in the camera scene?
Another workaround I thought was using the screen width and height and the regular clamp()
function as follows:
transform.Translate (h, v, 0);
Vector3 tmpPos = transform.position;
tmpPos.x = Mathf.Clamp (tmpPos.x, -Screen.width / 2, Screen.width / 2);
tmpPos.y = Mathf.Clamp (tmpPos.y, -Screen.height / 2, Screen.height / 2);
transform.position = tmpPos;
However I get the Screen.height
as 428, although the camera sees the interval of [-10, 10]. How am I supposed to clamp the movement independent of the game resolution, i.e. not using global positions but using relative positions so that I will be able to clamp correctly player's ship whatever the resolution of the game is?