The goal of this tutorial is to explain how you can implement an interaction between 2 characters with Unity 3d. One character is the player, the other one a so called NPC (= non player characters). If the player is near the npc, the interaction should start.
Let's assume we have these two characters, the player is the dog Erwin who is moving towards the Monster (Herbert):
The monster has a script attached, with the interaction logic added to the Update method:
using UnityEngine;
using System.Collections;
namespace Pathfinding
{
///
/// This class is used to implement the interaction between the monster "Herbert"
/// and the main character "Erwin" (dog).
///
/// The following used types are written by jayanam and not part of Unity5:
///
/// PlayerController: Class to control the player Erwin (Singleton)
/// ActionManager: Class with elper e.g. opening the communication panel to
/// show a character talking to another
///
public class Monster : MonoBehaviour
{
private Animator _animator = null;
void Awake()
{
_animator = GetComponent();
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
protected new void Update()
{
float dist = Vector3.Distance(PlayerController.Instance.transform.position, transform.position);
// We are near the monster => monster introduces himself
if (dist < 3.0f)
{
// Here you can add you own interactions...
// ActionManager.Instance.OpenTalkPanel(GameManager.Instance.Herbert,
// "Hi I am Herbert! Are you a friend?");
}
}
}
}
And here is a video in which you can see the script in action with an Animation Controller for both characters: