Moving the camera relative to the aspect ratio of the screen

TLDR: How do I make it so I can multiply the X and/or Z axis by 1.777777778f for the movement of my camera target, based on the camera’s current facing direction?

Right now my game has this pseudo isometric/top down style camera that you can rotate in 45 degree increments. The camera currently follows an empty game object that is a child to the character.

I’m trying to make it so the camera peeks a little forward based on the direction the character is moving, by having the character move that empty game object a little bit ahead of where it’s moving. But it needs to peek a little more forward when the character is moving left or right relative to the camera, since the viewport of course is a rectangle, this clip shows what I mean.

Currently I have it hard coded to apply that shift in the X Axis, so when the camera is facing North or South, it works as intended, but once I shift to East or West, you can see that it doesn’t work. This is the relevant code.

        Vector3 cameraTargetPosition = new Vector3(transform.position.x + moveDirection.x * ((cameraTargetLookDistance) * 1.77777778f), transform.position.y + moveDirection.y * cameraTargetLookDistance, transform.position.z + moveDirection.z * cameraTargetLookDistance);
        cameraTarget.transform.position = Vector3.SmoothDamp (cameraTarget.transform.position, cameraTargetPosition, ref cameraTargetVelocity, cameraTargetSmoothingTime * (cameraTargetReturnModifier - (speed * cameraTargetReturnModifier) + speed));

You kinda want the look point to be on a circle around the player, but that circle should stretch laterally depending on which way the camera is looking to give more advantage on the wider screen dimension.

One way is to flatten the CameraTransform.forward and the PlayerTransform.forward so both have no Y component, then normalize them and take the dot product.

You’ll get a number from -1 to 0 to +1: -1 is walking towards camera, +1 is walking away, and 0 is walking 90 degrees lateral.

You could look that up in a curve (such as an Animation Curve) to decide the distance ahead of the player to focus the camera, or just tween the math yourself, from 1.0 to 1.7778

1 Like

I’m not sure I fully follow all of this

I know my camera already sorta does some sort of dot product type deal to referential shift the player inputs so that the directions are all relative to the camera, but I got that code from a tutorial years ago and have just moved it project to project modifying it to work for what I need it to do, and I’m not 100% on how it actually does what it does anymore, or how it actually works mathematically

        Vector3 rootDirection = root.forward;

        Vector3 stickDirection = new Vector3 (horizontal, 0, vertical);

        //Get camera rotation
        Vector3 CameraDirection = camera.forward;
        CameraDirection.y = 0.0f; //kill y
        Quaternion referentialShift = Quaternion.FromToRotation(Vector3.forward, CameraDirection);

        //convert joystick input in Worldspace coordinates
        Vector3 moveDirection = referentialShift * stickDirection;

Is that basically what this part is doing? It seems like it is doing something similar to what you describe. How would I adapt that to what you are saying?

Animation Curves are another thing I am really unfamiliar with, I’ve never really used them and I’ve only really ever heard about them a couple of times, mostly because I’m not totally sure I understand them or how they work, so they are a little intimidating to me. But it does seem like a skill I need to learn.