- Home /
Help with NPC interact script
Hello, I am a newbie at coding so please bare with me. I followed a tutorial on cg cookie on how to create a npc interact script in unity, and it works for the most part, but when I enter the collider of the npc it does not show the first line of text. But if I hit Ctrl, then if moves on the the second, then the third, then the fourth, etc. How to I make it so that the first line of text appears when I enter the collider of the npc? Also something I would like help with. How would you make it so that the npc asks questions and lets you answer using Yes or no, and then responds to you accordingly. Thank you. Here is the script for the npc:
var talkTextGUI:GUIText;
var talkLines:String[];
var textScrollSpeed:int;
private var talking:boolean;
private var textIsScrolling:boolean;
private var playerScript:ThirdPersonController;
private var currentLine:int;
function OnTriggerEnter(col:Collider){
if (col.tag == "Player"){
playerScript = col.GetComponent(ThirdPersonController);
Debug.Log(playerScript);
talking = true;
currentLine = 0;
//talkTextGUI.text = talkLines[currentLine]; //STATIC
playerScript.enabled = false;
col.animation.CrossFade("idle");
}
}
function Update () {
if(talking)
if(Input.GetButtonDown("Ctrl")){
if(textIsScrolling){
//display full line
talkTextGUI.text = talkLines[currentLine];
textIsScrolling = false;
}
else{
//display next line
if(currentLine < talkLines.Length - 1){
currentLine++;
//talkTextGUI.text = talkLines[currentLine]; //STATIC
startScrolling();
}
else{
currentLine = 0;
talkTextGUI.text = "";
talking = false;
playerScript.enabled = true;
}
}
}
}
function startScrolling(){
textIsScrolling = true;
var startLine:int = currentLine;
var displayText:String = "";
for(var i:int = 0; i < talkLines[currentLine].Length; i++){
if(textIsScrolling && currentLine == startLine){
displayText += talkLines[currentLine][i];
talkTextGUI.text = displayText;
yield WaitForSeconds(1/ textScrollSpeed);
}
else{
return;
}
}
}
Here is the Level Manager script:
var talkTextGUI:GUIText;
var edgeMarginPercentage:int;
function Start () {
var edgeMargin:int = (Screen.width/100) * edgeMarginPercentage;
talkTextGUI.pixelOffset.x = edgeMargin;
talkTextGUI.pixelOffset.y = edgeMargin;
}
function Update () {
}
Your answer
Follow this Question
Related Questions
Enemy Database for RPG Battle System? 1 Answer
RPG style character with multiple items 1 Answer
How to organize NPC dialogs? 0 Answers
I need help fixing this code 1 Answer
NPC Party Member Direction 1 Answer