I’m trying to write a camera script which allows the player to view the scene from a bird’s eye view as seen in most Real Time Strategy games. The zoom function that moves the camera along the Y axis was not much of a problem.
I’m having troubles with the rotation and movement. For the latter I used the mouse movement so that the camera moves along the positive X axis as soon as the mouse hits the right edge of the game window. Left, Forward and Backwards are done similarly.
// Camera movement
private void HandleMovement()
{
Vector3 mousePosition = Input.mousePosition;
Vector3 movementDirection = Vector3.zero;
// Left
if (mousePosition.x <= leftBoundary)
{
movementDirection = Vector3.left * MovementSpeed;
}
// Right
else if (mousePosition.x >= rightBoundary)
{
movementDirection = Vector3.right * MovementSpeed;
}
// Backwards
if (mousePosition.y <= topBoundary)
{
movementDirection = Vector3.back * MovementSpeed;
}
// Forward
else if (mousePosition.y >= bottomBoundary)
{
movementDirection = Vector3.forward * MovementSpeed;
}
transform.Translate(movementDirection * Time.deltaTime, Space.World);
}
Due to the Space.World Parameter in Translate() the camera will always consider the global +Z axis as it’s forward direction. This means the camera won’t move forward into the direction it is facing when rotated around the Y axis but rather forward along the world’s Z axis.
Switching to Space.Self doesn’t seem to be a solution since the camera should always move orthogonal to the Y axis.
I was hoping that somebody could help me finding a solution for the camera’s rotation.
edit: Rotation Method & fixed title
private void HandleRotation()
{
if (Input.GetMouseButton(2))
{
currentMousePosition = Input.mousePosition.x;
float rotation = 0.0f;
if (currentMousePosition < previousMousePosition)
{
rotation = -RotationSpeed * Time.deltaTime;
}
else if (currentMousePosition > previousMousePosition)
{
rotation = RotationSpeed * Time.deltaTime;
}
transform.RotateAround(Vector3.up, rotation);
previousMousePosition = currentMousePosition;
}
}