Posted November 04, 2023 by Gasimo
#Development #Tech
Greetings everyone! Its time for some Gasimo Tech Tips, where I rant about some of the systems I have developed for Nightshift (some are open-sourced over at my github!). This one focuses on the interaction framework.
My system ditches raycasts for detection altogether and instead takes advantage of spherecasts (shooting a “sphere” shaped collider in the direction the player is looking at, registering every interactable which comes in its way).
RaycastHit[] hits;
hits = Physics.SphereCastAll(mainCam.gameObject.transform.position, discoveryRadius, mainCam.transform.forward, interactRange, InteractMask);
Then follows a step by step process (each of the steps includes an early exit if no interactable remains):
To make sure the system is modular and easy to scale, I took advantage of Interfaces. Defining common interaction types in Interfaces (IInteractable, IInventorySelector etc.) allows me to rapidly create new interactable objects in an object oriented way (just as our unnamed object-obsessed lord intended). If you ever create interaction system for any kind of game, definitely take advantage of this!
namespace Gasimo.Gameplay
{
interface IInteractable
{
// When player interacts with object
public void OnInteract(FirstPersonController pc);
// Return empty if you want object to not show up as interactable for a certain condition
public string GetTitle(FirstPersonController pc);
}
}