Dynamic Objects

Adding New Dynamic Objects

  1. To make an object dynamic (such as a door, drawer, lever, or valve), add a Dynamic Object component to the object.

  1. Select the object dynamic type by clicking on the icons located at the top of the component.

  2. Choose the Interact Type for the dynamic object, which can be either Dynamic, Mouse, or Animation type. Once you've selected the Interact Type, the References section will change, requiring you to assign all the necessary references to ensure the dynamic object works properly.

The Target field refers to the object where all interactions will be applied. This can either be the current object where the Dynamic Object component is added or a parent object.

Dynamic Door

  1. To configure a dynamic door, begin by setting the Open Limits, Hinge, and Forward Axis.

By using curves, you can specify the speed change when opening or closing the dynamic object. The curve should be defined between 0 to 1 values.

  1. Open Limits is actually an angle how the door should open. Rotate the door to its open position, note the hinge value, and update the Open Limits -> Max limit accordingly.

  1. Rotate the door back to the closed position. Adjust the Target Hinge (the axis where the door pivots) and Target Forward (the axis along which the door mesh extends).

Dynamic Drawer

If you select the Drawer dynamic type, you can determine the min/max open limits by copying the position from the corresponding axis, as demonstrated in the image below.

Setting Unlock Dynamic Script

To use a custom unlock dynamic script, create a script that implements the IDynamicUnlock interface. By doing so, you will be prompted to implement the OnTryUnlock() method, which will be called when you interact with the locked dynamic object. Here is a code snippet for a simple dynamic unlock script:

using UnityEngine;
using UHFPS.Runtime;

public class TestUnlock : MonoBehaviour, IDynamicUnlock, IInventorySelector
{
    public ItemGuid UnlockItem;
    private DynamicObject dynamicObject;

    public void OnTryUnlock(DynamicObject dynamicObject)
    {
        Inventory.Instance.OpenItemSelector(this);
        this.dynamicObject = dynamicObject;
    }

    public void OnInventoryItemSelect(Inventory inventory, InventoryItem selectedItem)
    {
        if (selectedItem.ItemGuid != UnlockItem)
            return;

        // unlock dynamic object
        dynamicObject.TryUnlockResult(true);
    }
}

If you interact with the locked dynamic object using this code, you will be prompted to use an item from your inventory. When you choose the item, OnInventoryItemSelect() will be triggered, which will unlock the dynamic object if the item is correct.

To use the dynamic unlock script, simply assign the reference to the script that implements IDynamicUnlock interface in the Unlock Script field.

A good example of using a custom unlock script is the lockpicking puzzle.

Last updated