# 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.

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2F8LqVu857AOZgFzuftwcR%2Fdynamic_obj.png?alt=media&#x26;token=44c190bc-cba3-48f8-b442-5cc19b0f0ec9" alt=""><figcaption></figcaption></figure>

2. Select the object dynamic type by clicking on the icons located at the top of the component.
3. 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.

{% hint style="info" %}
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.
{% endhint %}

### Dynamic Door

{% hint style="danger" %}
Make sure that the **Pivot Mode** is set to **Pivot** and **Pivot Rotation** to **Local**. If this will be set incorrectly, a prompt will appear.
{% endhint %}

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

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FvAmOxx5sK1sqDUSy34iy%2Fdyn_door.png?alt=media&#x26;token=e4d79271-be93-4a68-a39d-a20ab0c29d72" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
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.
{% endhint %}

2. **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.

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FdE15Hft9zo0wZVMycuc0%2Fdoor_open.png?alt=media&#x26;token=49ba3ad6-b6b8-4ddd-bdf1-8fd57d8f6b6e" alt=""><figcaption></figcaption></figure>

3. 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).

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2F8Ho1R8o7VYgMWvzbQEtA%2Fdoor_info_1.png?alt=media&#x26;token=19bbbca7-d422-4d54-b88c-09927a06a6d1" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}
Make sure you have gizmos enabled, it will help you set up the dynamic object correctly.
{% endhint %}

{% hint style="success" %}
In certain situations, if a door or custom openable model has a different pivot rotation, you can use axis mirroring to maintain positive axes. For instance, if an axis is set to X, toggling the mirror option will switch it to X\_Negative, and vice versa.&#x20;

The same principle applies to the **Starting Angle**, if the open position is reversed (e.g., -90 is open and 0 is closed), enabling mirroring will invert that angle as well.
{% endhint %}

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FKB9SNgxpz0EVG9m3nvSN%2Fmirroring.png?alt=media&#x26;token=53daac08-c545-4899-bfbf-884f5750a965" alt=""><figcaption></figcaption></figure>

### 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.

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FSfgvyWNw72t8H8qdbbvQ%2Fdyn_drawer.png?alt=media&#x26;token=506ac768-5322-4f9c-aed8-c8163d495d04" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
The process of configuring a switchable or rotatable dynamic type is similar to the previous steps.
{% endhint %}

### Setting Unlock Dynamic Script&#x20;

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 <mark style="color:blue;">**OnTryUnlock()**</mark> method, which will be called when you interact with the locked dynamic object. Here is a code snippet for a simple dynamic unlock script:

```csharp
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);
    }
}
```

{% hint style="info" %}
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.
{% endhint %}

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

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FiwNex808ClxHyVg6DBw3%2FScreenshot_7.png?alt=media&#x26;token=506a65c8-08d3-4bc6-90f3-d033a223895c" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
A good example of using a custom unlock script is the lockpicking puzzle.
{% endhint %}
