Game Development Reference
In-Depth Information
19. In
SetupMission2
present in the
Start()
method towards the end, add
a line of code to pass the ID of the question, which this NPC will give the
player, into the
QuizNpcHelper
. This way, when it's time to tell the NPC to
change its animation, the pop-up card can find its NPC by searching for the
one with the correct ID:
QuizNpcInstance.GetComponent<QuizNpcHelper>().SetQuizNpcId(k);
20. Inside
InteractiveObject
, we add a simple
billboarding
behavior to
the script so that
QuizNpc
can always face the player. If the billboard check-
box is enabled, we use the
Transform.lookat()
built-in method to make
sure the NPC's transform always faces the player. We also fix up the local
angles to makes sure the NPC doesn't look up or down if the NPC itself is
really close to the player:
if (billboard == true)
{
GameObject player =
GameObject.Find("Player1");
if (player != null)
{
this.transform.lookat(player.transform.position);
this.transform.localEulerAngles =
new Vector3(0.0f,
this.transform.localEulerAngles.y, 0.0f);
}
}
Congratulations!
QuizNpc
now animates nicely and celebrates when the player re-
sponds correctly.