- Home /
Camera Script - Bird's Eye View
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;
}
}
In your code, I do not see you rotate the camera. Is there something missing?
Answer by helmi77 · Feb 16, 2012 at 03:55 PM
I finally found a solution to this problem without altering the rotation method at all. The only thing I edited is the last line of the movement method from
transform.Translate(movementDirection * Time.deltaTime, Space.World);
to
transform.localPosition += transform.localRotation * movementDirection;
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers
Edit this code? 0 Answers
Move the camera to the left when the mousecursor is left 1 Answer
No Glitch Please ... 1 Answer