Adding a New Interactable Object

Let's add a new type of interactable object that we can grab, hold, and throw (it will be inherited from the VRInteractable component) with an additional feature: some visual effect (for example, smoke) will appear when we grab the object and, it will disappear when we release the object.

  1. Create a new VRObjectVFX component and add the following code to it:

    Source code (C++)
    
    #pragma once
    #include <UnigineComponentSystem.h>
    #include <UnigineLog.h>
    #include "vr/Components/VRInteractable.h"
    
    class VRObjectVFX : public VRInteractable, public Unigine::ComponentBase
    {
    public:
    	COMPONENT_DEFINE(VRObjectVFX, Unigine::ComponentBase);
    	COMPONENT_INIT(init);
    	COMPONENT_UPDATE(update,);
    
    	// *.node asset containing the effect
    	PROP_PARAM(File, vfx_asset);
    
    	// interaction methods
    	void grabIt(VRPlayer* player, VRController::Side side) override;
    	void throwIt(VRPlayer* player, VRController::Side side) override;
    
    protected:
    	void init();
    	void update();
    
    private:
    	// internal 'grabbed' state
    	bool grabbed = false;
    	// vfx-node created from the specified asset
    	Unigine::NodePtr vfx;
    };
    						
    Source code (C++)
    
    #include "VRObjectVFX.h"
    
    using namespace Unigine;
    
    REGISTER_COMPONENT(VRObjectVFX);
    
    void VRObjectVFX::init()
    {
    	// check if the asset with the visual effect is specified
    	if (vfx_asset.nullCheck())
    	{
    		Log::error("Node %s (VRObjectVFX) error: 'vfx_asset' is not assigned.\n", node->getName());
    		return;
    	}
    	else
    	{
    		// if specified, create a new node from the specified *.node asset
    		vfx = NodeReference::create(FileSystem::guidToPath(FileSystem::getGUID(vfx_asset.getRaw())));
    
    		// and hide this node (effect)
    		vfx->setEnabled(false);
    	}
    }
    
    // this method is called for the interactable object each frame
    void VRObjectVFX::update()
    {
    	// update transformation of the effect if the object is grabbed
    	if(grabbed)
    		vfx->setWorldTransform(node->getWorldTransform());
    }
    
    // method to be called on grabbing a node
    void VRObjectVFX::grabIt(VRPlayer* player, VRController::Side side)
    {
    	// set the current object state to 'GRABBED'
    	grabbed = true;
    
    	// show the effect
    	vfx->setEnabled(true);
    }
    
    // method to be called on throwing (releasing) a node
    void VRObjectVFX::throwIt(VRPlayer* player, VRController::Side side)
    {
    	// turn the 'GRABBED' state off
    	grabbed = false;
    
    	// hide the effect
    	vfx->setEnabled(false);
    }
    						
  2. Select the cylinder (NodeReference) node and click Edit to modify its contents. Add the VRObjectVFX property to the cylinder(ObjectMeshStatic) node.

  3. Drag the vr/particles/smoke.node asset to the Vfx Node field. This node stores the particle system representing the smoke effect. It is available in the vr/particles folder of the UNIGINE Starter Course Projects add-on.

  4. Select the cylinder (NodeReference) node again and click Apply to save your changes to the NodeReference.

    Save changes (Ctrl+S) and run the application via SDK Browser.

Now, if you grab and hold the cylinder, it will generate smoke:

The information on this page is valid for UNIGINE 2.22 SDK.

Last update: 2026-09-11 03:51:51