hey I am fallowing the Zelda livestream tutorial which is a good tutorial series. I am having a little
trouble with the player Interacting with an npc. here is part of the code:
Collider2D[] closecollider = Physics2D.OverlapCircleAll (transform.position, 2f);
for (int i = 0; i < closecollider.Length; ++i) {
Interactable coliderInteract = closecollider[i].GetComponent<Interactable>();
if(coliderInteract == null){
continue;
}
Vector2 directtointeract = closecollider[i].transform.position - transform.position;
I can’t see anything wrong with this script but it is just a snippit.
Can you:
- Provide more of the script
- Provide the compiler error, if that is your problem, or else a description of what behavior you expect and what you are seeing instead.
well I want the player to face the object or npc
using UnityEngine;
using System.Collections;
public class Interactmodle : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnInteract(){
Interactable useableinteract = Finduseableinteract ();
if (useableinteract == null) {
return;
}
useableinteract.OnInteract ();
}
Interactable Finduseableinteract(){
Collider2D[] closecollider = Physics2D.OverlapCircleAll (transform.position, 2f);
for (int i = 0; i < closecollider.Length; ++i) {
Interactable coliderInteract = closecollider[i].GetComponent<Interactable>();
if(coliderInteract == null){
continue;
}
//this is where I want the player to face the npc.
Vector2 directtointeract = closecollider[i].transform.position - transform.position;
Debug.Log (i + ":" + closecollider[i].name);
}
return null;
}
}
So you want to make the user face the NPC but they are not doing that?
Well,
Vector2 directtointeract = closecollider[i].transform.position - transform.position;
this line of code is calculating the vector from the player to the collider we are interacting with. However, you are not actually turning the player to face in that direction.
I’m guessing this script is on the player, so I think you can replace the above line with this:
transform.LookAt(closecollider[i].transform);
However, this may not have the same behavior as the author originally intended since it does not require the calculation. Perhaps you skipped a line in the tutorial?