Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Help with making an interaction system!

Discussion in '2D' started by BlackMucus, Jan 23, 2022.

  1. BlackMucus

    BlackMucus

    Joined:
    Jul 16, 2020
    Posts:
    5
    Hello, I'm a beginner in Unity and would like some help developing an interaction system. I have some experience working in Unity but have been on and off about it. I mostly copy scripts from videos and in this case it was this one. Since I am making a top-down 2D game, I had to come up with my own solution as to making the interaction feel a bit better. It's not great, but gets the job done, as this is mostly just me trying to learn unity in my own way.
    Anyway, I can interact with the 'npc' I made, but I want to expand on this interaction system. The ability to interact with objects and npcs and being able to get different results from each interaction. I feel like I am basically asking for people to code my game for me since it sounds like it would take multiple scripts, but help me out here, I'm new.

    I copied the script from the video so the are more or less the same, but in case you want to see for yourself: upload_2022-1-23_17-45-55.png
    Maybe my solution is incorrect, or I'm thinking of this wrong, but I'm thinking maybe somehow I can make it have a different interaction based on the layer it is on. AKA, an npc layer, then an object layer. Am I thinking of this right? I have little experience with C#, let alone programming in general (learned C# first but got really confused, eventually settled on Python which made more sense to me and stuck with it for a few months, going back to C# was easier since the fundamental concepts are similar)

    This is the first actual 'game' I've made in a while so I'm actually pretty happy so far that I'm learning and actually making something and understanding the code I'm copying from better, but I'd love the extra help. Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Welcome! I respect that... getting our hands dirty is what it's all about.

    Let me tell you about interfaces in C# and under Unity.

    The idea would be to have interfaces for things that are "interactable" or "examinable" or "collectible".

    If you had a machine that implemented the interactable interface, you could also have an NPC that implements the same interface, but responds completely differently.

    Interfaces can be as light as zero functions, which makes them almost a label or tag.

    Or for collectible the interface method might be simply "Collect()"

    Or they can be as heavy as saying "interactable means it has methods to tell you who he is, what he can do, you can tell him to do stuff, etc."

    The idea is a player would check for an object to have the interactable or collectible interface and then talk to that object through the given interface. In the future as you add more objects, a) the player doesn't change, and b) the existing objects don't have to change.

    Let me leave you with some canned links about using Interfaces in Unity3D:

    https://forum.unity.com/threads/how...rereceiver-error-message.920801/#post-6028457

    https://forum.unity.com/threads/manager-classes.965609/#post-6286820

    Check Youtube for other tutorials about interfaces and working in Unity3D. It's a pretty powerful combination.
     
    JustinOaksford and BlackMucus like this.
  3. BlackMucus

    BlackMucus

    Joined:
    Jul 16, 2020
    Posts:
    5
    Thank you! This is all very helpful! I'll definitely be checking out those links as well.