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. Dismiss Notice

Discussion Creating NPCs that intereact with eachother...

Discussion in 'Scripting' started by secretbench, Aug 31, 2023.

  1. secretbench

    secretbench

    Joined:
    Jun 9, 2022
    Posts:
    11
    I would like to create a store with NPCs that are costumers and NPCs that are working there. I want them to occasionally interact; in a situation where a costumer need to ask a question or a clerk might attempt a hard sale. All interactions will be very basic, no or little logic about their interactions will be stored. Interaction of "costumer question", would go something like, the customer and the employee would have a short conversation in gibberish and both have a end of interaction emotion or animation displayed; then both return to there main tasks of shopping for customer or work related task queue for the store associated.

    Store associates would be at their post or performing a task such as stocking or cleaning; some logic such as tasks completed and tasks in queue will be stored and tracked.

    Customers would enter the store, shop the isles, eventually checkout and leave the store; these NPCs would simply be destroyed, none of their logic is important to outcome of the game).

    All of this will serve as a backdrop for a scene I am working on in which the player will have to move through this store without being detected by associates. So the associates will also have field of view logic and a means of calculating their focus vs distraction to determine if they notice the player.

    I am using net code as the game will have co-op features.
    I am using unity navmesh for navigation.
    I am using a recent Unity 2022 build for dev.

    Is there a simple or recommended path to achieve this or am I starting from scratch?
    Any ideas or recommendations?
     
    Last edited: Sep 1, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Google can find you some great tutorials.

    Screen Shot 2023-08-30 at 8.12.58 PM.png


    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    If you mean make a scene where npc's are just appearing to walk from vendor to vendor, or pretend to be shopping? Most games I've noticed really don't go in depth with little stuff like that, it's just normally pre-programmed routes and animation wait times. Because if you ever watched those npc's you'll notice they do the same thing over and over, with following the same set of a simple loop.

    So I wouldn't think to hard on it, just make it simple and pre-programmed as much as possible.

    Unless you mean you want their interactions to be random, then that would require way more programming. Or a simple cheese of having multiple "interactions" for "npc1", "npc2", etc... Then just randomize what npc gets what loop. So one play session might be the woman in the green dress who argues with the clerk. And another play session, the woman in green dress is now washing the bucket behind stall 3, etc...

    But if the game is about the player, and all he's doing is sneaking through the scene, the AI backdrop really just needs to be simple stuff. :)
     
    secretbench likes this.
  4. secretbench

    secretbench

    Joined:
    Jun 9, 2022
    Posts:
    11
    Yes that is spot on. I was just curious if there were any behavior systems available I might consider using. Or am I best to create a goap system from scratch. It's certainly not outside of my wheelhouse to do so, just wondering if there's anything anyone recommends to use instead of going it alone.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    There are plenty of NPC and behavior tree assets on the Unity Asset Store, as well as others you can get from github repos.

    I have no idea if any of these assets would help your problem space but I do know that if you use one, you will now need to understand how that system works too.

    You may wish to consider trying this approach:

    Imphenzia: How Did I Learn To Make Games:

     
  6. secretbench

    secretbench

    Joined:
    Jun 9, 2022
    Posts:
    11
    I feel as though everyone is answering this as a question. I meant for it to be a discussion. Also surprised no one linked to any useful topic related examples, while there is a whole section in unity learn and multiple assets on the store.The first responder came out to say, "google it", which I can only imagine is their way of responding to all topics?

    Please lets discuss this. What best practices would you employ when creating a finite state machine? For this scenario would you use the GOAP architecture? Are there any readily available pre-made assets you'd recommend? why?

    Edit: I didn't mean to cast shade, my apologies
     
    Last edited: Sep 1, 2023
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Yes there are solutions, pre-made assets, as well as best practices, tutorials, example code, etc.. Are you in a position to evaluate their suitability to your purpose? Have you even evaluated even one approach and implemented it (even partially) to see if it suits your needs?

    If you have not, I suggest perhaps you are not equipped to benefit from a conversation about all the different approaches available.

    It is how I answer a complex question when the poster has clearly not written a single line of code towards the problem, and yet has a laundry list of complex needs and specifications:

    - pathing
    - dialogs
    - a pending work task queue
    - stealth
    - networking
    - hooked into a quest system

    Thus I stand by my post #5 above. Get to work! Everything you do will be one step at a time, just like me.
     
    secretbench likes this.
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    It sounds like you already have a good idea about how to implement this already. You might just ho ahead and implement what you've planned-out so far and see if it works. Then you can expand upon it later.
     
    secretbench likes this.
  9. secretbench

    secretbench

    Joined:
    Jun 9, 2022
    Posts:
    11
    Correct, I do and am currently creating and using a finite state machine in my project. I just wanted to hear from others on how they would do it. I'm starting to think I posted in the wrong place or that this community has no interest in discussions?
     
  10. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    Why? Lot's of people responded. What were you expecting that you didn't get?
     
    secretbench and Kurt-Dekker like this.
  11. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    Absolutely not. To me it just sounds like what you're describing here is a simple special effect- just an animated background. No need to make it complicated. ;)
     
    secretbench likes this.
  12. secretbench

    secretbench

    Joined:
    Jun 9, 2022
    Posts:
    11
    Thank you, this is more what I was looking for. I am currently working on a GOAP style Finite State Machine. Are you suggesting I script the NPC's to just walk loops with predefined locations along the floor and then just exit the stage when they have finished their loop. No AI, just programed movements?
     
  13. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    Something like that. I'd start with a simple loop, but maybe I'd expand that to a loop with randomly-selected branching paths, so it's not the same each time. I should mention, though, that I'm still working these things out for my own game so I don't have all the answers myself. :D

    I tend to implement things incrementally, though, instead of trying to come-up with a complete solution from the beginning. That's why I initially suggested just going forward with your first idea.

    I could be wrong, but I feel like making an AI agent will create complexity that just won't come across to the player's perspective. Just simple randomness is often effective.

    This reminds me of an anecdote:
    I made a little simulation one time where there were trilobites in an aquarium, each one a random size.

    The AI was real simple- At random intervals of 3-8 seconds it chose one of three random behaviors: 70% it would sit still and fiddle with it's antennae, 25% it would move forward with a slight random rotation, 5% it would "swim" which is the same as move forward except the walk animation was really fast and there was a slight upward force applied.

    A lot of interesting and unexpected things would happen: If the trilobite walked in to the walls of the tank, it would look like it was trying to climb up the side and would fall upside down, which I didn't expect. Whenever the trilobite tried to "swim" when it was upside down, you could see all those legs flailing around in the water as it rocked up and down. people I showed it to thought it really looked like it was struggling to right itself- like it was aware that it was upside down. This was completely unintentional.

    Also the big trilobites would push the little trilobites out of their way, but if two big trilobites met they would fight each other for supremacy. That's what people thought, anyway. Really they were just moving randomly with no awareness of each other. Only the dynamic rigidbodies where causing any interaction.

    So the interesting thing I took away from the experience is that people will ascribe intelligent behavior to randomness if there is any possible interpretation for it.
     
    secretbench and Kurt-Dekker like this.
  14. secretbench

    secretbench

    Joined:
    Jun 9, 2022
    Posts:
    11
    This is a really interesting examination. I guess it is the psychological equivalent to human tendency to personify inanimate objects with anthropomorphized features; even lending a small amount of empathy to non living object.

    I may play around with some randomness to see how it looks.
     
  15. secretbench

    secretbench

    Joined:
    Jun 9, 2022
    Posts:
    11
    First I am sorry if I offended you or seemed ungrateful. I appreciate that you took the time to read my post. I also did not realize you had posted twice.

    Perhaps my post was not well explained. I am using the FSM described in the unity learn example here:
    https://learn.unity.com/project/goal-driven-behaviour
    While it did not fit all of my needs, it is easy to extend to meet my specifications. I am rather attached to using it for this project. I am not new to FSMs or game logic and would like to discuss how others have achieved the same goal. If somebody has an idea that is simpler to implement or overall better at achieving my end goal, I would probably investigate; I am always looking to improve my knowledge base.

    All of this said, I did reread my post and it does sound questioning in a way that warrants the response I got. I wrote it late at night and was tired and wanted to post and go to bed.
     
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I am not offended in any way. That's inside your head. This is just communication and engineering.

    That's AWESOME! That's the START of a conversation!!!

    To really get a technical conversation rolling, here might be a follow-on you could add to the statement above:

    "I find with this approach that doing X is really easy, but doing Y is much harder and requires me to combine information from many different unrelated things, which feels wrong to me."

    or perhaps...

    "I am using an FSM to store what state each NPC is in, such as a shopkeeper being busy selling something to villager #1. But then I'm having a problem because Villager #2 now has to know that the shopkeeper is busy..."

    This is what a technical discussion might begin with. This could then "snag" people who read your post and they'll say "Oh man I had this problem, here's how I dealt with it!!!" or maybe even "Package ABC from the asset store handled this beautifully for me, here's a link!"

    That's now technical discussions get off the ground and going.

    Otherwise, seeing yet another undifferentiated post to discuss a complex multi-axis topic, my answer will always be boilerplate.