In the HitScanWeapon Script
Add an export variable for weapon model
Assign the SMG mesh to the variable
Return to the HitScanWeapon script
add a variable for recoil distance
Refactor the process function
Move shooting behaviour into its own function
move the gun back by the recall distance
If you test the game now, each time you fire the gun the model will move back a little bit.
Now we need to smoothly move it back to its starting position.
To move the gun back to the starting position we will use Linear Interpolation (Lerp)
In games, "lerp" (linear interpolation) is a function that smoothly blends or transitions between two values over time, such as moving a character between positions, changing an object's colour, or animating a UI element. It works by calculating a value along a straight line between a start and an end value, based on a third parameter that represents the progress of the interpolation.
Set the starting position of the weapon
By adding a ready variable
Add a line to move the gun model
Back to its starting position
Because this is in the process function
This will happen repeatedly.
Add a RayCast3D node as a child
X: 0.0m Y: 0.0m Z: -100.0m
This casts a horitzontal ray
100 m out from the player
To see what the ray has collided with
Add a print statement to the
Now when you fire the weapon
You will get output of what it hit.