Top Down Camera in Unity – Unity By Example

Top Down Camera in Unity

The top down camera is used in games such as Civilization, Warcraft 3, XCOM, TAVERN MASTER, PRISON ARCHITECT, etc.d

Steps

  • Create script TopDownCamera.cs
using UnityEngine;

public class TopDownCamera : MonoBehaviour
{
    [SerializeField] private int _moveSpeed;

    private void Update()
    {
        Vector3 mouse = Input.mousePosition;
        if (mouse.x <= 0 || Input.GetKey(KeyCode.A))
        {
            Move(Vector3.left);
        }
        else if (mouse.x >= Screen.width || Input.GetKey(KeyCode.D))    
        { 
            Move(Vector3.right);
        }

        if (mouse.y <= 0 || Input.GetKey(KeyCode.S))
        {
            Move(Vector3.back);
        }
        else if(mouse.y >= Screen.height || Input.GetKey(KeyCode.W))
        {
            Move(Vector3.forward);
        }
    }

    private void Move(Vector3 position)
    {
        transform.Translate(position * _moveSpeed * Time.deltaTime);
    }
}
  • Add script to top down object and move Main Camera to the object

Result

You can use keyboard or mouse to control

Download

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments