I’m trying to make a modular interact system that works like that:
1- All interactable Actors (like a door, a light switch, a shelf…) have a class component called InteractableActor;
2- When the player looks for the interactable actor and press “F” key, the InteractableActor class attached to the interacted actor will call a function that I have previously defined at the details panel.
Example: the player looks at the door and press “F” key, so the door oppens. Then he looks at the light switch and press “F” key, so the light switch turns on.
CONCLUSIOIN: I need to have a field in the details panel that allows me to select a function of interaction for each type of interactable object.
I’m migrating from Unity Engine, and there I used a code like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class InteractableObject : MonoBehaviour
{
public UnityEvent onInteract; //The Inspector panel in Unity shows a field to attach a function.
public void Interact() //When player press "F" key, this function is called.
{
onInteract.Invoke();
}
}
- All interactable Actors (like a door, a light switch, a shelf…) have a class component called InteractableActor;
Create an Interface InteractableActor with Interact() function and implement it in your Actors
- When the player looks for the interactable actor and press “F” key, the InteractableActor class attached to the interacted actor will call a function that I have previously defined at the details panel.
When you “collide” with your Actors check if they implement your InteractableActor. Then call Interact().
- I need to have a field in the details panel that allows me to select a function of interaction for each type of interactable object.
In your Door Actor just implement open/close inside Interact() function, same for your Light switch.