In the game we're creating, the PC can approach various NPCs and initiate a text-based conversation with them. What i'd like to do is when the player starts a conversation with an NPC, have that NPC turn to face the player. I've got it half working: The NPC successfully rotates the first time but not any time after that, despite calling my rotateTowards function from within Update(). I thought it was a to do with my use of triggers (the player would have to exit the trigger for it to update, I thought), but i'm now using a raycast and it still exhibits the same problem.
The relevant excerpt from the player interaction script:
if (Input.GetButtonDown("Interact")) {
if(!npcDialogue.isTalking){
print("I am attacking");
GetComponent(playerCombat).performAttack();
}
var hit : RaycastHit;
// if we cast a ray forward and it hits something
if(Physics.Raycast(transform.position, transform.forward, hit)){
if(hit.collider.gameObject.tag == "npc"){
currentNPC = hit.collider.gameObject;
var dist = Vector3.Distance(currentNPC.transform.position, transform.position);
if(dist <= talkProximity){
print("I am talking to "+currentNPC.name);
currentNPC.GetComponent(npcDialogue).startConversation();
}
}
}
}
The NPC dialogue script:
var myName : String;
var myDialogue : String[];
private var currentLine : int = 0;
static var isTalking : boolean = false;
var myRespawn : GameObject;
private var player : GameObject;
var rotationSpeed = 3.0;
// artificial gravity since we are not using a rigidbody
var gravity = 20.0;
// variable to store the player's current forward direction (set to 0,0,0 at initialisation)
private var moveDirection = Vector3.zero;
function Start(){
player = GameObject.Find("Player Character");
}
function Update(){
if(isTalking){
RotateTowards(player.transform.position);
print("Called rotate");
}
// Subtract the value of gravity once a second to bring the player back down to the ground
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
// store a reference to the CharacterController component of this object in a variable called 'controller'
var controller : CharacterController = GetComponent(CharacterController);
// move the controller along the heading specified by moveDirection and in metres per second (store this movement in a variable called 'flags')
var flags = controller.Move(moveDirection * Time.deltaTime);
}
function startConversation(){
if(!isTalking){
currentLine = 0;
dialogueGUI2.mySpeaker = myName;
dialogueGUI2.showDialogue = true;
isTalking = true;
moveChar.allowMove = false;
} else {
if(currentLine < myDialogue.Length-1){
currentLine++;
} else {
isTalking = false;
dialogueGUI2.showDialogue = false;
moveChar.allowMove = true;
player.GetComponent(respawnControl).saveProgress(myRespawn);
}
}
dialogueGUI2.myDialogue = myDialogue[currentLine];
}
function RotateTowards (position : Vector3) {
position.y = 0;
//var targetRotation = Quaternion.LookRotation(position - transform.position, Vector3.up);
var targetRotation = Quaternion.LookRotation(position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}