Recoil on every shot
Updated
Holding the left mouse button fires every 0.12 seconds, and each shot kicks the view through the camera’s Camera Recoil effect.
Hold the left mouse button over the view to fire and watch the view climb.
Recoil on every shot
#pragma lifecycle(startup, update)
const FIRE_INTERVAL = 0.12; // seconds between shots while the button is held
let recoil;let cooldown = 0;
function startup() { recoil = self.parent.getComponent(CameraRecoilEffectComponent);}
function update(dt) { cooldown -= dt; if (Input.mouse.isButtonPressed(MouseButton.Left) && cooldown <= 0) { cooldown = FIRE_INTERVAL; recoil.play(0.5); emit('weapon/fired'); }}How it works
Section titled “How it works”The script is explained step by step in Recoil on every shot.
Open it in XR Creator
Section titled “Open it in XR Creator”Download the project file below, then import it in XR Hub with Projects › New › Import, as described in Projects and templates. It opens in XR Creator with the same scene you see running above, each script already attached to its Behavior entity. From there you can edit the scripts, adjust components and their props, preview your changes, and deploy the result as a world of your own.
RecoilGun.xrproj