Confusion about the calculation

switch (sender)
        {
            case ShootAction shootAction:
                Unit shooterUnit = shootAction.GetUnit();
                Unit targetUnit = shootAction.GetTargetUnit();

                Vector3 unitHeight = new Vector3(0, 1.7f, 0);

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

                float shoulderOffsetAmount = .5f;
                Vector3 shoulderOffset = Quaternion.Euler(0f, 90f, 0f) * shootDir * shoulderOffsetAmount;

                Vector3 calcCamPos = shooterUnit.GetWorldPosition() + unitHeight + shoulderOffset + (shootDir * -1);
                actionCameraGameObject.transform.position = calcCamPos;

                actionCameraGameObject.transform.LookAt(targetUnit.GetWorldPosition() + unitHeight);

                ShowActionCam();
                break;
        }

I’m very confused about the whole quaternion calculation, i think very slightly understand how it works, but i fail to understand why this is done instead of a simple vector calculation.

Initially i thought the lookDir was to look at the target but we do a simple transform.lookAt at the end.
So my question is, why did we opt for this calculation instead of a simple virtualCamGameObject.transform.position = new Vector3(xOffset, characterHeight, 0);
or even just have an empty gameObject in the unit and set the cam to that position.

I must be missing something, thanks in advance!

1 Like

We offset the camera to the right of the unit. Now we need the camera to look at the target, but because of the offset, the direction from the camera to the target is not the same as the direction from the unit to the target.
Also see this topic: Understanding Action Camera shoulder positioning

1 Like

It certainly looks like it should be that simple. I’ll be the first to admit that Quaternion math was not my strong suit. What I can tell you is that this formula applies the shooting direction (shootDir) to the calculation so that the position is always in the same position relative to the back of the shooter’s head regardless of the way that the shooter is facing. This is trickier to accomplish without the Quaternion library, using only Vector3.

Privacy & Terms