using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RPG.Dialogue
{
public class AIConversant : MonoBehaviour
{
[SerializeField] Dialogue dialogue = null;
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Player")
{
if(Input.GetKeyDown(KeyCode.E))
{
GetComponent<PlayerConversant>().StartDialogue(dialogue);
}
}
}
}
}
Attached the script to my AI and select the dialogue but nothing happens when I enter the trigger area and press “E”. Do I also need to modify my PlayerConversant.cs?
I just check with Debug.Log and it is not even triggering. The collider is set as trigger yes. Any ideas? The player and the playermesh are set with tags “Player”. The yellow cube is the NPC.
OnTriggerEnter fires in the frame that the player entered the trigger. Once. In that frame. GetKeyDown returns true in the frame that the player pressed the key. Once. In that frame. The only way this will work is if the player pressed the key in exactly the same frame as the character entered the trigger area, and the odds of that happening are pretty small (not impossible).
Try using OnTriggerStay instead. See the documentation here
I think one of the objects needs to have a Rigidbody to register a collision. The colliders just set the boundaries for collisions. The Rigidbody is what detects the collision has occurred.