Search Unity

Character to NPC Interaction

Discussion in 'Scripting' started by DRRosen3, Nov 28, 2014.

  1. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    I'm trying to get the ball rolling with my game's dialogue. I don't want to do like I see in most of the tutorials (that I've found) on the web where you use a trigger collider. I'd much rather prefer that if the player gets close enough to an NPC and pushes a certain button, that it initiates a conversation with the NPC. Anyone have any suggestions on how I can go about (starting) to achieve this?
     
  2. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    Use the trigger collider like the tutorials you've watched, but rather than simply firing off the dialogue function in the OnTriggerEnter function, use OnTriggerStay and then an if( Input... (etc.) to ask for the input.

    In other words, you can use the trigger collider to determine that the player is close enough, and then a second piece of logic to actually begin the dialogue.
     
    DRRosen3 likes this.
  3. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Code (CSharp):
    1. if(Vector3.Distance(characterPosition, npcPosition) < someDistance && Input.getKeyDown(interactKey))
    2. {
    3.     runInteractionScript();
    4. }
    As for the interaction, I would have the interaction disable the player & switch your GUI state to an npc prompt, taking the ID of the npc you're talking to as the parameter. Good luck with stuff.
     
    DRRosen3 likes this.
  4. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Thanks you two. That's enough to get me rolling. Really appreciated!