Question by
korsanbilgisayar · May 26, 2020 at 04:12 PM ·
ontriggerenter
GameObject Follow the Player on trigger enter
I have making a small game for my school project. What I want to do is; I have multiple patrolling ghosts in my scene and i want to rescue them one by one. when player is in range and press E, I want that ghost character follows the player so i can take him to rescue area.
using UnityEngine;
using UnityEngine.AI;
public class GhostCatch : MonoBehaviour
{
GameObject player;
NavMeshAgent ghost;
bool touchIng = false;
// Start is called before the first frame update
void Start()
{
ghost = GetComponent<NavMeshAgent>();
player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update()
{
if (touchIng && Input.GetKeyDown(KeyCode.E))
{
ghost.destination = player.transform.position;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
touchIng = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
touchIng = false;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D not fired while OnTriggerStay2D is 1 Answer
Killing enemy jumping on his head 2 Answers
OnTriggerEnter is giving me nothing 1 Answer
OnTriggerEnter work only once 0 Answers