Need help with my gun movement script

So I have been following a tutorial series by ETeeski Tutorials, and I got stuck on fps 1.8. AKA scripting a basic gun with weight.

This is the script:

#pragma strict

var cameraObject : GameObject;

var rotationSpeed : float = 0.3;

@HideInInspector
var targetXRotation : float;

@HideInInspector
var targetYRotation : float;

@HideInInspector
var targetXRotationV : float;

@HideInInspector
var targetYRotationV : float;

var holdSide : float = 0.5;

var holdHeight : float = -0.5;

function Update () {

transform.position = cameraObject.transform.position + Quaternion.Euler(0, targetYRotation, 0) * Vector3(holdSide, holdHeight, 0);

targetXRotation = Mathf.SmoothDamp(targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotationSpeed);
 
targetYRotation = Mathf.SmoothDamp(targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotationSpeed);

transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
}

I understand that the u set the position of the gun to follow the camera’s position and then move it to a certain position using the Vector3. But if you are going to set the rotation of the Vector3 here, why use the variable “targetYRotation”? I don’t really understand this part of the code and all help will be appreciated. Thanks!

Sometimes the easiest way to figure out code is to change it. So take out the ‘Quaternion.Euler(0, targetYRotation, 0)’ on line 25 and see what happens. Here is a diagram to visualize what is going on:

13663-explain.png

The blue dot is the position of the player (cameraObject.transform.position). The green dot is the position of the gun. The black line is the calculation ‘Vector3(holdSide, holdHeight, 0)’ The ‘Quaternion.Euler(0, targetYRotation, 0)’ calculation swings the gun vector around the axis I’ve indicated in red. That is without this code, the gun would not point in the same direction as the camera is looking. It would be at a fixed position relative to the world axes.