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 VRBaseInteractable 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. In addition, it will display certain text in the console if the corresponding option is enabled.

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

    Source code (C#)
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Unigine;
    
    [Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
    public class VRObjectVFX : VRBaseInteractable
    {
    	// an asset with an effect that occurs when the object is grabbed
    	public AssetLinkNode vfx_node = null;
    	private Node vfx = null;
    
    	// this method is called for the interactable object each frame
    	private void Update()
    	{
    		// update transformation of the effect if the object is grabbed
    		if(CurrentState == VRBaseInteractable.INTERACTABLE_STATE.GRABBED)
    		vfx.WorldTransform = node.WorldTransform;
    	}
    
    	// this overridden method is called after component initialization (you can implement checks here)
    	protected override void OnReady()
    	{
    		// check if the asset with the visual effect is specified
    		if (vfx_node == null)
    		{
    			Log.Error($"{nameof(VRObjectVFX)} error: 'vfx_node' is not assigned.\n");
    			Enabled = false;
    			return;
    		}
    		else
    		{
    			vfx = vfx_node.Load();
    			vfx.Enabled = false;
    		}
    	}
    
    	// this overridden method is called when grabbing the interactable object
    	public override void OnGrabBegin(VRBaseInteraction interaction, VRBaseController controller)
    	{
    		// show the effect
    		vfx.Enabled = true;
    		// set the current object state to 'GRABBED'
    		CurrentState = VRBaseInteractable.INTERACTABLE_STATE.GRABBED;
    	}
    
    	// this overridden method is called when releasing the interactable object
    	public override void OnGrabEnd(VRBaseInteraction interaction, VRBaseController controller)
    	{
    		// hide the effect
    		vfx.Enabled = false;
    		// set the current object state to NOT_INTERACT
    		CurrentState = VRBaseInteractable.INTERACTABLE_STATE.NOT_INTERACT;
    	}
    }
  2. Assign the VRObjectVFX.cs component to the cylinder node (the child of the kinematic_movable dummy 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. Save changes (Ctrl+S) and press the Play button to run the application.

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

Last update: 2024-08-16
Build: ()