Create a drum that plays a sound and an animation when hit

This article was written for “UniVCI-0.15.”

Example data: https://seed.online/items/d4a40607b5b0906b7dba095731766838059f6ff04334d9a62957957e9c3ed9be

Audio sample:

Let's create a drum that plays a sound and an animation when hit by a stick.
We will be using “onCollisionEnter” as we will be executing the process when objects collide with each other.

This VCI is comprised of two VCIs: a drum and a stick.

To play animations and sounds, you need to prepare assets in addition to a script.
Let's do this step-by-step.

1. Prepare an audio sample
2. Prepare an animation
3. Prepare colliders
4. Write a script

1. Prepare audio sample
To play a sound, prepare an audio sample
Even if you use compressed audio files, they will be exported as uncompressed wav when exporting as a VCI.
Be aware as the file size may get larger.
Also, there may be some cases where audio can be played on Unity's preview, but not playable on Virtual Cast due to the format of the audio.
If this is the case, try using other file formats.

Attach the audio file as an Audio Source on the root VCI Object.

2. Prepare an animation
To create an animation file, create an Animation Clip on the root VCI Object.

You can choose to enable/disable the loop of the animation.
If the animation is set to one-shot (loop disabled), the shape of the object will be fixed in the end state of the animation.
In this example, we've created an animation that changes the scale of the drum to make the drum larger for a split second and go back to the original size.
As with the audio source file, attach the Animation on the root VCI Object.
Be aware that the animation needs to be registered as element 0~ in the Animations.

3. Prepare colliders
We need to configure colliders and rigidbodies as we need to detect physical collision.
When attaching colliders, make sure to put all colliders on the root SubItem.
When the object moves, colliders attached on objects under the root SubItem will be scattered around.

We configured each SubItems as shown below.
Values especially important are…
- Use Gravity
- Is Kinematic
- Is Trigger
- Grabbable
Remember the functions of each setting.

Specify the same number other than 0 to the GroupID parameter of all SubItems.
SubItems with the same GroupID will have their ownership transferred together to the grabber of a SubItem,
allowing for proper calculation of physics.

4. Write a script
On the VCI Object component in the GameObject, set the Scripts Size as 1.
Enter “main.lua” in the Name and paste the script shown below.

function onCollisionEnter(item, hit)
    if (item == "drum" and hit == "stick") or (item == "stick" and hit == "drum") then
        vci.assets._ALL_PlayAudioFromName("drum_snd") --Play a sound
        vci.assets._ALL_PlayAnimationFromName("drum_anm",false) --Play an animation, set the enable/disable of loop with true/false
        vci.assets.HapticPulseOnGrabbingController(item, 1500, 0.05) --Rumble the controller which is grabbing the item
en/vci/sample/oncollision/no1.txt · Last modified: 2022/09/05 18:02 by pastatto

Page Tools