• Register

The scientist Dr. Zachery developed a time machine, we are in the year 2048. Dr. Zachery’s pet, the dog Erwin, examines the machine in the Dr.'s laboratory and triggers the mechanism for starting the timetravel. When Dr. Zachery enters his lab he sees his time machine running and Erwin is gone... lost in time. The only chance for Dr. Zachery to bring Erwin back is to produce little wormholes in the environment where Erwin is currently located and send him parts of the time machine. Erwin has to fnd these parts, put the machine together to come back to his owner. The game is a top-down cartoon adventure in which the player has to solve puzzles, avoid traps, collect and repair objects to find the scattered parts of the time machine in many different worlds and times: The middle ages, a district in 1984 or ancient Asia. The game is in concept stage, the first level is nearly completed.

Post tutorial Report RSS Unity3d character interaction

In this tutorial we will explain character interaction with Unity 3d and C# code.

Posted by on - Intermediate Client Side Coding

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):

erwin and 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:

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: