This page has been translated automatically.
UNIGINE 基础课程
1. 简介
2. 虚拟世界管理
3. 3D模型准备
4. 材质
5. 摄像机和光照系统
6. 实现应用程序逻辑
7. 制作过场动画与动画序列
8. 准备发布项目
9. 物理系统
10. 优化基础
11. 项目2:第一人称射击游戏
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:

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

Last update: 2024-11-06
Build: ()