Hello everyone.
I’m currently working on a point and click adventure game, and I’m at a point where I’m not sure how to continue.
Basic idea is your regular adventure game. You have a character that you control by clicking into the world and onto interactable items.
Currently, I’m in the business of getting used to Unity itself, so I’ve created the various components I’ll be needing. For movement, I’ve used a simple navmesh with a raycast from a click. Now I am looking into the event system and was thinking that it would probably be best to do every user interaction with it. That would mean that I would need to have an event on every item in the scene, which seems like a lot.
So I was thinking that it might make sense to have an event on all the objects in the scene that are interactable, and then a sort of catchall for any click made in the world that does not hit an event triggered object that would make the character move.
The only other way I can think of is to always raycast and identify the object I hit, and if it has a script to interact with, I’ll do that, otherwise I move. But that seems a bit sloppy.
Does anyone have any advice, or is the latter really the best way to go?
Eh, I would just add a tag “Interactable” and manually add it to all interactable game objects. When you raycast, just see if you hit an object with that tag.
Then, I would create a simple interface IInteractable which would represent a contract to all interactable items in the game. Whenever you want to create a new interactable action, have it implement that interface and add it to the object you want. Then, when you raycast and hit an object with an “Interactable” tag, just do its action. Something like this:
public interface IInteractable()
{
void DoInteraction();
}
public class PickupItem : MonoBehavior, IInteractable
{
public override void DoInteraction()
{
// Add the object to the inventory
// Remove the object from the scene
// etc.
}
}
public class PlayerInput: MonoBehavior
{
private void Update()
{
if(RaycastHitInteractableTag, out hitGameObject)
{
hitGameObject.GetComponent<IInteractable>().DoInteraction();
}
}
}
1 Like
Ah yes, using a tag is a great idea, thank you. So basically my current solution, a bit polished up, would still be the best, you think?
Yeah, I would do it like this for starters. Don’t over-engineer stuff. If you need it right now or in the very new future, build it, if not, don’t.