Interactive Objects

Modified on Thu, 25 May 2023 at 10:19 AM

Voxel Play provides a base class that you can use for your interactive objects.

This class allows your gameobject to receive a notification when the player approaches, goes away or triggers some action on the object (usually pressing key E).


A door is a perfect example for an interactive object. You can find a few Doors voxels in the sample world. The door voxel is a custom voxel whose prefab has a Door script:


This door script inherits from VoxelPlayInteractiveObject as follows:


    public class Door : VoxelPlayInteractiveObject {
    ...
    }


An object that inherits from VoxelPlayInteractiveObject has the following fields and methods:


public string customTag;
A public field that you can use to tag this object. For example in a double door, you can differentiate the left door from the right door using this field.


public float interactionDistance = 4f;
The maximum distance from which the player can trigger an action on this object.


public bool triggerNearbyObjects;
Use this option to trigger nearby objects when this one is activated. For example when you open a double door triggering the left door, it will also open the right door.


public bool playerIsNear;
True if the player is near the object.


protected VoxelPlayEnvironment env;
You can use this field to access Voxel Play Environment API from the object itself.


public virtual void OnStart() {}

You can override this method to add initialisation code to your interactive script.


public virtual void OnPlayerApproach() {}

Called when the player approaches (see interactionDistance)


public virtual void OnPlayerGoesAway() {}

Called when the player goes away (see interactionDistance)


public virtual void OnPlayerAction() {}

Called when player triggers the object (ie. press key E on the object)


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article