Isometric Camera Movement

Hi everybody,

I’m trying to create an 2D RTS Game like Warcraft 2 with isometric map.
I have some problems to move correctly my camera.

When I move down my mouse the camera is out of the map . I would like increase xmin to resolve the problem.
but when i increase xmin it doesn’t work and it’s not smoothly.

Here the code of my camera

public class CameraManager : MonoBehaviour {
    public float speed = 0.5f;
    
    private Vector3 startPos;
    private bool moving;
    private float xmin = 450;
    private float xmax = 22031;
    private float ymin = -3713;
    private  float ymax = 160;
    
    void Start() {
    Screen.SetResolution (1280, 720, true);
    
    }
    void FixedUpdate() {
           
    if (Input.GetMouseButtonDown (1)) {
        startPos = Input.mousePosition;
        moving = true;
    }
    if (Input.GetMouseButtonUp (1) && moving) {
        moving = false;
    }
    if (moving) {
        float vertical =  Input.GetAxis("Mouse Y");
        float horizontal = Input.GetAxis("Mouse X");
        // Clamp Value for camera
        Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - startPos);
         
        Vector3 move = new Vector3 (pos.x * speed, pos.y * speed,0 );
        transform.Translate (move, Space.Self);
        
        Vector3 clampedPosition = transform.position;
       
        Debug.Log(xmin);
        clampedPosition.x = Mathf.Clamp (transform.position.x, xmin, xmax);
        clampedPosition.y = Mathf.Clamp (transform.position.y, ymin, ymax);
       
        transform.position = clampedPosition;
    }
    }

and here the code of my map:

public class GameManager : MonoBehaviour {
    public Sprite GrassTile;
    public GameObject Bricks;
    public GameObject Panel;
    public GameObject Mushroom;
    public GameObject Root;
    public GameObject Flowers;
    public TextAsset textFile;
    private int tileType;
   
    private const int columns = 448;
    private const int rows = 248;
    public int[,] mapData = new int[columns,rows];
   
    void Start () {
    // Read and split text
    char[] Separator = new char[] {',','\n'};
    string text = textFile.text;
    string[] lines = text.Split (Separator);
   
    int index = 0; // index for read and parse lines array
    int x = 0;
    int y = 0;
    int isoX = 0;
    int isoY = 0;
    for(int j=0; j < rows; j++) {
        for(int i =0; i < columns; i++){
        mapData[i,j] = Int32.Parse(lines[index++]);
        x = i * 32;
        y = j * -32;
        isoX = (x - y);
        isoY = (x + y) /2;
           
        /*if (mapData[i,j] == 46) {
            Instantiate(GrassTile,new Vector3(isoX, isoY, 0),Quaternion.identity);   // Orthographics
          
        }*/
       
        var tile = new GameObject();
        tile.transform.position = new Vector3(isoX,isoY,1);
        var spriteRenderer = tile.AddComponent<SpriteRenderer> ();
        spriteRenderer.sprite = GrassTile;
        tile.name = "Grass" + tile.transform.position;
       
        if (mapData[i,j] == 62) {
            Instantiate(Flowers,new Vector3(isoX,isoY, 0),Quaternion.identity);
         
        }
        if (mapData[i,j] == 135) {
            Instantiate(Root,new Vector3(isoX,isoY,0),Quaternion.identity);
        }
       
        if (mapData[i,j] == 282) {
            Instantiate(Bricks,new Vector3(isoX,isoY ,0),Quaternion.identity);
           
        }
        if (mapData[i,j] == 103) {
            Instantiate(Panel,new Vector3(isoX,isoY,0),Quaternion.identity);
        }
        if(mapData[i,j] == 280) {
           Instantiate(Mushroom,new Vector3(isoX,isoY,0),Quaternion.identity);
        }
       
        }
    }
    }
       
    // Update is called once per frame
    void Update () {
       
    }
}

Sorry for my bad english :wink:
Thank you for your help.
Bye

Normally the map moves not the camera, for example you move up, and the map moves down, what type of camera are you using.

Oh yes piggybank I can use offset to move my map instead my camera.
I use Orthographic camera with no rotation. I tried with 45 and 30 degrees but it doesn’t work for me… I don’t know why.

Thank for your help

@captainchris

It is easier to move the camera mind you, you need a move script, that is NOT connected to the player but follows the player around, are here it is on my Camera component script.

I’ve recoded for you and taken out some stuff I have.

Attach this to the Camera Object, and drag your Hero/Player GameObject over the Hero name in the inspector.

public class CameraComponent : MonoBehaviour
{
public GameObject Hero;

void Update ()
{
  if (mHero != null)
    {
     transform.position = Vector3.Lerp(transform.position, Hero.transform.position, 0.1F) + new Vector3(0, 0, -10);
    }
}
}

this will give you a slow camera movement around the Hero/Player.

1 Like