Action Camera for Sword Action

If you want an action camera like the shoot action for the sword action, you can add the code below in the CameraManager script in private void BaseAction_OnAnyActionStarted(object sender, EventArgs e)

case SwordAction swordAction:
                Unit swordUnit = swordAction.GetUnit();
                Vector3 cameraCharacterHeightSword = Vector3.up * 1.7f;

                Vector3 swordDir = swordUnit.GetWorldPosition().normalized;

                //float shoulderOffsetSwordAmount = 0.5f; //Add if wanted
                //Vector3 shoulderSwordOffset = Quaternion.Euler(0, 90, 0) * swordDir * shoulderOffsetSwordAmount;

                Vector3 actionCameraSwordPosition =
                    swordUnit.GetWorldPosition() +
                    cameraCharacterHeightSword +
                    //shoulderSwordOffset +
                    (swordDir * -1);

                actionCameraGameObject.transform.position = actionCameraSwordPosition;
                actionCameraGameObject.transform.LookAt(swordUnit.GetWorldPosition() + cameraCharacterHeightSword);

                ShowActionCamera();
                break;

Then add this code in private void BaseAction_OnAnyActionCompleted(object sender, EventArgs e)

case SwordAction swordAction:
     HideActionCamera();
     break;
3 Likes

Very nice, thank you! I was going to look into doing this myself.

One thing I notice, (at least the way things are set up for my version of the project, which is how they’re done in the course) is that the camera only faces the proper direction when the Unit using the SwordAction is already facing ‘forward’ toward their target. For example: if the Unit is on GridPosition(0,0) and their target is on GridPosition(0,1). If I were to move the Unit to GridPosition(0,2) first and then attack the same target, the camera doesn’t rotate to adjust for the direction of the attack.

The solution for this is already in the ShootAction case and all it needs is a refrence to a target Unit (which also means exposing the targetUnit from SwordAction) and using it in the swordDir Vector3:

Unit swordTargetUnit = swordAction.GetTargetUnit();
Vector3 swordDir = (swordTargetUnit.GetWorldPosition() - swordUnit.GetWorldPosition()).normalized;
1 Like

Yup exactly, the swordDir cannot just be the swordUnit.GetWorldPosition(); it needs the direction towards the target so that the offset is in the correct position
Nice job!

1 Like

Privacy & Terms