en:vci:sample:shooting:no1

Table of Contents

Shooting Game

Shoot the opponent's balloon. Grabbing a balloon will reset the state.
Ammo will be gone after shooting 10 times. Hitting a magazine on the gun will reload the gun.

Example data

Component setting

Components for each object are as shown below.

As we need to accurately calculate the collision of your balloon and the opponent's bullet,
GroupIDs of blue bullet, blue gun, blue magazine and red balloon is set to 1;
Group IDs of red bullet, red gun, red magazine, and blue balloon is set to 2.

VCI script

main.lua
--Acquire SubItems
gun_B=vci.assets.GetSubItem("gun_B")
gun_R=vci.assets.GetSubItem("gun_R")
bullet_B=vci.assets.GetSubItem("bullet_B")
bullet_R=vci.assets.GetSubItem("bullet_R")
 
ammo_limit_B=10--Bullets left
ammo_limit_R=10
 
---[Requires ownership of SubItem]Called when a Collider (not Trigger) collides with the item.
---@param item string @SubItem name
---@param hit string @Collider name
function onCollisionEnter(item, hit)
    if (item=="target_B" and hit == "bullet_R") or (item=="bullet_R" and hit=="target_B") then--The blue balloon exploded
        vci.assets._ALL_PlayAudioFromName("hit_B")
        vci.assets._ALL_PlayAnimationFromName("hit_B",false)
    elseif (item=="target_R" and hit == "bullet_B") or (item=="bullet_B" and hit=="target_R") then--The red balloon exploded
        vci.assets._ALL_PlayAudioFromName("hit_R")
        vci.assets._ALL_PlayAnimationFromName("hit_R",false)
    elseif (item=="gun_B" and hit == "ammo_B") or (item=="ammo_B" and hit=="gun_B") then--Blue reload
        vci.assets._ALL_PlayAudioFromName("reload")
        vci.assets.HapticPulseOnGrabbingController(item, 500, 0.3)
        ammo_limit_B=10
    elseif (item=="gun_R" and hit == "ammo_R") or (item=="ammo_R" and hit=="gun_R") then--Red reload
        vci.assets._ALL_PlayAudioFromName("reload")
        vci.assets.HapticPulseOnGrabbingController(item, 500, 0.3)
        ammo_limit_R=10
    end
end
 
 
---[Requires ownership of SubItem] Called when the grip button is pressed while being grabbed.
---@param use string @Name of the SubItem being used
function onUse(use)
    --When the grip button is pressed, set the position of the ammo to the tip of the gun and add force
    if use=="gun_B" then --The blue gun
        --Launching a bullet
        if ammo_limit_B>0 then--ammos left
            ammo_limit_B=ammo_limit_B-1 --ammo minus 1
            vci.assets._ALL_PlayAudioFromName("shoot")
            vci.assets.HapticPulseOnGrabbingController(use, 2000, 0.1)
            bullet_B.SetLocalPosition(gun_B.GetLocalPosition())
            bullet_B.SetLocalRotation(gun_B.GetLocalRotation())
            bullet_B.SetVelocity(Vector3.zero)--Set the speed of the rigidbody 0
            bullet_B.SetAngularVelocity(Vector3.zero)--Set the rotation speed of the rigidbody 0
 
            local houkou=gun_B.GetForward()--The forward of the ammo
            bullet_B.AddForce(5000*houkou)
        else--if no ammos left
            vci.assets._ALL_PlayAudioFromName("noammo")
            vci.assets.HapticPulseOnGrabbingController(use, 500, 0.05)
        end
 
    elseif use=="gun_R" then --The red gun
        if ammo_limit_R>0 then--ammos left
            ammo_limit_R=ammo_limit_R-1 --ammo minus 1
            vci.assets._ALL_PlayAudioFromName("shoot")
            vci.assets.HapticPulseOnGrabbingController(use, 2000, 0.1)
            bullet_R.SetLocalPosition(gun_R.GetLocalPosition())
            bullet_R.SetLocalRotation(gun_R.GetLocalRotation())
            bullet_R.SetVelocity(Vector3.zero)--Set the speed of the rigidbody 0
            bullet_R.SetAngularVelocity(Vector3.zero)--Set the rotation speed of the rigidbody 0
 
            local houkou=gun_R.GetForward()--The forward of the ammo
            bullet_R.AddForce(5000*houkou)
        else--if no ammos left
            vci.assets._ALL_PlayAudioFromName("noammo")
            vci.assets.HapticPulseOnGrabbingController(use, 500, 0.05)
        end
 
    elseif use=="target_B" then --Reset target: blue
        vci.assets._ALL_PlayAnimationFromName("reset_B",false)
    elseif use=="target_R" then --Reset target: Red
        vci.assets._ALL_PlayAnimationFromName("reset_R",false)
    end
end
en/vci/sample/shooting/no1.txt · Last modified: 2023/05/11 20:05 by pastatto

Page Tools