Repositioning the camera (side view)?

I want the camera to shift from a top-down camera for the game to a side view during the actual attack. I am using Cinemachine & I have two cameras made. They are successfully transitioning between the two.

Where I am struggling is how to position the camera properly. Here is what I have so far:

[SerializeField] private GameObject actionCameraGameObject;
[SerializeField] private GameObject cameraTestObject;

Unit shooterUnit = shootAction.GetUnit();
Unit targetUnit = shootAction.GetTargetUnit();

//I don't think I need this line anymore?
Vector3 shootDir = (targetUnit.GetWorldPosition() - shooterUnit.GetWorldPosition()).normalized;

//This code find the midpoint (x & z) between the two units
Vector3 center = (shooterUnit.GetWorldPosition() + targetUnit.GetWorldPosition())/2f;

//This is to rotate the camera, but isn't quite working.  I think it is what I am multiplying it by that is causing issuess?
Vector3 shoulderOffset = Quaternion.Euler(0,90,0) * center;

//This code declares how high the camera should be
Vector3 cameraCharacterHeight = Vector3.up * heightOfCamera;

//This is defining the camera location
Vector3 actionCameraPosition = center + shoulderOffset + cameraCharacterHeight;
    
//This is moving the camera to the correct position            
actionCameraGameObject.transform.position = (actionCameraPosition);

//This is rotating the camera to the correct location
actionCameraGameObject.transform.LookAt(center);

//I am using this to visualize where the camera was placed...this will be deleted later
Instantiate(cameraTestObject, actionCameraPosition, Quaternion.identity);

If I remove the rotation line, it places the camera perfectly between the two characters, but I need something in there so that the camera:

  1. Is rotated from a side view, as opposed to a top view

  2. Is far enough back so that both units are visible.

Any suggestions?

1 Like

You want it to look from the side where the attacker is on the left side of the screen and the target is on the right side?
I think your logic is mostly correct, I think the only issue is when you’re applying the rotation. You’re applying the rotation to the center world position when you should instead apply that to the shootDir
So for the shoulderOffset (which I guess should be called sideOffset) it should be Euler(0, 90, 0) * shootDir * Vector3.Distance(targetUnit.GetPosition(), shooterUnit.GetPosition());
That will rotate 90º relative to the shoot direction, so it will be off to the side and it will move in that direction by the distance between those two units.

I think the only thing missing beyond that would be setting the FoV. You need to do some math to figure out what FoV you need to make sure both characters are visible on screen. Or keep the same Fov and just move further or closer

2 Likes

No need to figure out the FoV - your code suggestion did it for me :slight_smile:

The camera is adjusting, so that both characters are both comfortably on screen. I’ll leave photos below, in case anyone wants to use it in the future.

Thank you so much!

Normal View:

Action Shot:

Updated Code:

                Unit shooterUnit = shootAction.GetUnit();
                Unit targetUnit = shootAction.GetTargetUnit();

                Vector3 shootDir = (targetUnit.GetWorldPosition() - shooterUnit.GetWorldPosition()).normalized;

                //This code find the midpoint (x & z) between the two units
                Vector3 center = (shooterUnit.GetWorldPosition() + targetUnit.GetWorldPosition())/2f;

                //This code sets the rotation of the camera to be from the side
                Vector3 shoulderOffset = Quaternion.Euler(0,90,0) * shootDir * Vector3.Distance(targetUnit.GetWorldPosition(), shooterUnit.GetWorldPosition());

                //This code declares how high the camera should be
                Vector3 cameraCharacterHeight = Vector3.up * heightOfCamera;

                Vector3 actionCameraPosition = 
                    center + 
                    shoulderOffset +
                    cameraCharacterHeight;
                
                actionCameraGameObject.transform.position = (actionCameraPosition);
                actionCameraGameObject.transform.LookAt(center);

                //Instantiate(cameraTestObject, actionCameraPosition, Quaternion.identity);
2 Likes

Those 2D sprites look great, good job!

Privacy & Terms