Unity5 character interaction script
In the last tutorial we created the monster character with the 3 eyes in Blender, now I import him to our Unity5 game Erwin’s timewarp.
What I would like to show you is the interaction of the main character, the dog Erwin, with the monster and how you can code this with C# in Unity3D.
So we start with two rigged characters dropped into the scene of Unity3D:
We assign a Character Controller to the monster Herbert and an Animation Controller:
In the image above you can see that we assigned a Seeker script. This component is from the A* Pathfinding framework we described in this tutorial. We used this framework to let the monster rotate to the direction were the player is located – the monster is following the movement of the player.
To achieve this, we need to derive the script for the monster from the class AIPath of the A* framework, here is the whole Monster-script:
<pre>using UnityEngine; using System.Collections; using Assets.ErwinsTimewarp.Scripts; using Assets.ErwinsTimewarp.Scripts.Characters; namespace Pathfinding { /// <summary> /// 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 /// </summary> [RequireComponent(typeof(Seeker))] public class Monster : AIPath { private Animator _animator = null; public float sleepVelocity = 0.4F; protected override void Awake() { base.Awake(); _animator = GetComponent<Animator>(); } public override Vector3 GetFeetPosition() { return tr.position; } // Use this for initialization protected override void Start() { base.Start(); target = PlayerController.Instance.PlayerTransform; } // Update is called once per frame protected new void Update() { if (GameManager.Instance.IsDead) return; //Get velocity in world-space Vector3 velocity; if (target != null) { // Calculate desired velocity Vector3 dir = CalculateVelocity(GetFeetPosition()); // Rotate towards targetDirection (filled in by CalculateVelocity) RotateTowards(targetDirection); dir.y = 0.0f; float dist = Vector3.Distance(PlayerController.Instance.transform.position, tr.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?"); } } else { velocity = Vector3.zero; } } } }
And this script also contains the interaction logic: When the dog (Erwin) is near the Herbert (distance < 3.0f), Herbert talks to him. He tells him that his name is Herbert and he asks Erwin if he is a friend.
The class to show the communication panel in the lower screen area is not a part of Unity, we have written it by ourselves. Basically it sets the visible-property of a Unity UI panel to true for a period of 3 seconds, that’s all. But you can add your own interaction code there, it is just an example.
We plan to add a feature that Erwin can have a conversation with other characters for such parts of the game.
The animation controller is very simple: Herbert is doing an Idle animation in which his eyes are moving and he is breathing, therefore there is only one animaton in the controller, the Idle animation:
Here is a video in which you can see the script in action and get some more explanations:
That’s it, if you have any question feel free to ask.