My current project that my friend and I created has no name yet. But we know what it will look like. We drew the inspiration from FF Tactics, so we wanted to have an isometric world with 2D sprites.
We could use any 2D isometric world game engine with Tiled support for example, but then we also wanted to be able to rotate the camera. So 3D engine it is.
I put together a small scene in Unity where I have:
- Isometric camera view (orthogonal in camera settings)
- Rotate camera script
- 3D world made of cubes
- 2D sprite that always looks at the camera
I thought making 2D sprite looks at the camera is going to be the hardest part, but actually making the camera rotate according to input was trickier.
For the sprite’s script, I used:
transform.LookAt(target);
Where the target is camera.
For camera’s rotation, I needed to know:
- Which coordinate is the camera rotating around.
- Which way should the camera rotate to.
- How to move the camera.
- How to stop the camera.
So here’s my solution:
I used raycast from center of the camera to determine the pivot point.
Camera camera = GetComponent<Camera>();
Vector3 cameraCenter = camera.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.height/2, camera.nearClipPlane));
Vector3 fwd = transform.TransformDirection(Vector3.forward);
Vector3 targetLocalPoint = new Vector3();
RaycastHit info;
if (Physics.Raycast( cameraCenter, fwd, out info ))
targetLocalPoint.z = info.distance;
I have input called “CameraRotate”, so I can use it to determine the direction.
_rotateAngle = ( 90f * Input.GetAxis("CameraRotate") );
Then I move the camera with:
transform.RotateAround(_targetPos, Vector3.up, angleDiff);
And stop the rotation when it reached the target angle.
Next, I need to add code to change the sprite’s frame when the camera rotated to emulate 3D view.
Leave a comment