Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Other Game over when ai touches you

Discussion in 'Navigation' started by pacims, May 6, 2022.

  1. pacims

    pacims

    Joined:
    Apr 30, 2022
    Posts:
    1
    Hello so i'm making a horror game and i have this script which makes the ai follow you so
    could anyone help so when the player would touch the ai the game would end.

    Script:


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;

    public class EnemyChase : MonoBehaviour
    {
    public NavMeshAgent enemy;
    public Transform Player;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    enemy.SetDestination(Player.position);
    }
    }
     
  2. Mohammed28_

    Mohammed28_

    Joined:
    Apr 23, 2022
    Posts:
    11
    You can add a cillider to the Agent and make it a trigger you can then restart the game or show lose menu when triggered.

    And for that to work, you will need to make sure both the player and enemy have a collider, and give the player a tag, it's best to use "Player", but you can use any tag you like.

    Example:

    Code (CSharp):
    1. private void OnTriggetEnter(collider col)
    2. {
    3.     if(col.gameObject.tag == "Player")
    4.     {
    5.         loseMenu.setActive(true);
    6.     }
    7. }
    But trigger will be called when player goes in agent, so you can try:

    Code (CSharp):
    1. private void OnCollisionEnter(collision col)
    2. {
    3.     if(col.gameObject.tag == "Player")
    4.     {
    5.         loseMenu.setActive(true);
    6.     }
    7. }
    I am typing in phone so I might have misspelled some of the code.

    Also, just an advice; try searching in google before posting in forum.

    Edit:
    If you want to make the player lose when enemy touch, make sure to disable isTrigger in the collider.