This page has been translated automatically.
Unigine Basics
1. Introduction
2. Managing Virtual Worlds
3. Preparing 3D Models
4. Materials
5. Cameras and Lighting
6. Implementing Application Logic
7. Making Cutscenes and Recording Videos
8. Preparing Your Project for Release
9. Physics
10. Optimization Basics
11. PROJECT2: First-Person Shooter
12. PROJECT3: Third-Person Cross-Country Arcade Racing Game

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 "../Framework/Components/VRInteractable.h"
    class VRObjectVFX :
        public VRInteractable
    {
    public:
    	COMPONENT(VRObjectVFX, VRInteractable);
    	COMPONENT_INIT(init);
    	COMPONENT_UPDATE(update,);
    
    	// property name
    	PROP_NAME("VRObjectVFX");
    
    	// *.node asset containing the effect
    	PROP_PARAM(File, vfx_asset);
    
    // interaction methods
    	void grabIt(VRPlayer* player, int hand_num) override;
    	void throwIt(VRPlayer* player, int hand_num) 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());
    		ComponentSystem::get()->removeComponent<VRObjectVFX>(node);
    		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, int hand_num)
    {
    	// set the current object state to 'GRABBED'
    	grabbed = true;
    
    	// show the effect
    	vfx->setEnabled(true);
    }
    
    // method to be called on trowing (releasing) a node
    void VRObjectVFX::throwIt(VRPlayer* player, int hand_num)
    {
    	// 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:

Last update: 2024-12-13
Build: ()