I have a 3D scene with some quads and a sprite
my camera is setup to look at the scene at a 45º angle like so
now I have the camera following the sprite around, and it all works as expected.
But I am not happy with the perspective, so I found that a nice correction was to alter the transform matrix or (view matrix) of the camera, fixing the tilt of the sprite by basically billboarding it (and avoiding geometry clipping) and skewing the geometry to create a pleasing effect
the problem is that after I alter the values of the matrix, the camera follow stops working correctly and the character starts going further than the bounds of the camera
when I alter the m11 component of the matrix, jumping on the Y axis eventually sends the character offscreen
when I alter the m12 component, walking north eventually sends the character offscreen
here is the code I am using for camera following, and for changing the view matrix of the camera
// Public fields to assign the actual camera and dummy camera
public Camera camz;
public Camera dummyCam;
// Target that the camera should follow
public Transform target;
public Vector3 positionOffset;
// Matrix offsets exposed as public fields for modification in the Inspector
public float offset00, offset01, offset02, offset03;
public float offset10, offset11, offset12, offset13;
public float offset20, offset21, offset22, offset23;
public float offset30, offset31, offset32, offset33;
void Update()
{
// Update the dummy camera's position to follow the target
dummyCam.transform.position = target.position + positionOffset;
// Get the current worldToCameraMatrix from the dummy camera
Matrix4x4 currentMatrix = dummyCam.worldToCameraMatrix;
// Apply offsets to the current matrix
currentMatrix.m00 += offset00; currentMatrix.m01 += offset01; currentMatrix.m02 += offset02; currentMatrix.m03 += offset03;
currentMatrix.m10 += offset10; currentMatrix.m11 += offset11; currentMatrix.m12 += offset12; currentMatrix.m13 += offset13;
currentMatrix.m20 += offset20; currentMatrix.m21 += offset21; currentMatrix.m22 += offset22; currentMatrix.m23 += offset23;
currentMatrix.m30 += offset30; currentMatrix.m31 += offset31; currentMatrix.m32 += offset32; currentMatrix.m33 += offset33;
// Apply the modified matrix to the actual camera's worldToCameraMatrix
camz.worldToCameraMatrix = currentMatrix;
}
It seems the distortion I am creating on the camera is messing with the math that keeps the character in the center of the screen
I was wondering if someone knew if there was some formula I could use to compensate the offsets I am inserting and allow the camera to keep up with the character
sorry for the long post and thanks for your time