- Home /
Isometric - Camera follow an object when at the edge of the screen
Hi!
I'm brand new to Unity and want to create an Isometric turn-based game like Final Fantasy Tactics, Disgaea, etc. (at least a prototype to train myself).
The problem I'm facing here is that my camera follows my cursor on each of its movement as you can see here, while I would want the camera to start following the cursor only when it is at the edges of the screen.
I'm using an Isometric Z as Y grid with isometric tilemaps with it. I also have the cursor GameObject, which is the object I want to follow, moving with WASD.
Here are the components and parameters of my camera:
And here's the CameraController script to follow the Cursor:
using UnityEngine;
public class CameraController : MonoBehaviour
{
public GameObject cursor;
private Vector3 offset;
private void Start()
{
offset = transform.position - cursor.transform.position;
}
void LateUpdate()
{
transform.position = cursor.transform.position + offset;
}
}
For this script, I followed the basic script found in the Unity documentation, but I tried a lot of different way to try and get the result I'm trying to achieve, which didn't really worked well.
I also watched and read a lot of things about camera movement but still didn't really understood everything, so I'm kind of lost on how I should approach this.
So could it be possible to somehow calculate or get the edges of what the camera sees to make it follow the cursor when it reaches them?
Answer by bpaynom · Feb 18, 2019 at 07:42 PM
You can make use of WorldToVIewportPoint and check if your cursor is in some width / height ratio. For example, you could check if the cursor position (as viewport point) is in x<0.1 or x>0.9 and the same for y. So the margin would be 10% of the width and 10% of the height, and when the cursor go inside those margins, you update the camera position.
Oh I didn't know I could do that, I'm gonna go take a look at it and try to do something, thanks!
Your answer
Follow this Question
Related Questions
Isometric Prespective camera drag/drop 0 Answers
Main Camera child under player, causes flip 3 Answers
Many cameras with Camera.main.ScreenToWorldPoint trouble. 2 Answers
2D camera panning and can't click UI buttons 0 Answers
Camera wrapping 0 Answers