ENEMY AI COMPLETE (kind of)


I finally added enemy AI that works like a charm.

after 0.5s it will chase after you

if not in view for more than 10 seconds they all return to their original position

no script changes needed to add more bots

doesn't chase you like a dumb ass.

all in one script (has other stuff) accept for one script to detect if your in their view  

this script is on a camera

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

public class movetoplayer : MonoBehaviour
{
    public bool collis;
    public bool temp;
    public bool follow;
    public bool check;
    public GameObject cam;
    public GameObject player;
    public GameObject enemy;
    public NavMeshAgent agent;
    public float positionx;
    public float positiony;
    public float positionz;
    public float rx;
    public float ry;
    public float rz;
    public float chck = 0.0f;
    public float spinspeed = 50.0f;
    public float rotationattime = 0.0f;
    public bool timeup;
    public float waittime = 0.5f;
    public float chasetime = 10.0f;
    public bool nien;
  

    // Update is called once per frame
    private void Start()
    {
        positionx = transform.position.x;
        positiony = transform.position.y;
        positionz = transform.position.z;
        rx = enemy.transform.eulerAngles.x / 3;
        ry = enemy.transform.eulerAngles.y - 360;
        rz = enemy.transform.eulerAngles.z / 3;
        check = false;
    }
    void Update()
    {
        Debug.Log(rx);
        
        Debug.Log(ry);
        Debug.Log(rz);
        
      
        Debug.Log(rx);
        if (follow == true)
        {
            agent.SetDestination(player.transform.position);
            check = true;
            chck = chck + 1;
        }
        else
        {
            if (agent.remainingDistance <= agent.stoppingDistance)
            {
                if (chck > 0)
                {
                    enemy.transform.eulerAngles = new Vector3(rx, ry, rz);
                }
                

            }
        }
        

        
        Debug.Log(follow);
        if (collis == true)
        {
            
            Vector3 direction = player.transform.position - transform.position;
            Quaternion targetRotation = Quaternion.LookRotation(direction);
            Quaternion lookAt = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * 1000);
            transform.eulerAngles = new Vector3(lookAt.eulerAngles.x, lookAt.eulerAngles.y, lookAt.eulerAngles.z);
            RaycastHit hit;
            if ((Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, 100)))
            {
                Debug.Log(hit.transform.name);
                if (hit.transform.gameObject == player)
                {
                    nien = true;
                    
                    StartCoroutine(wait());
                   
                    if (timeup == true)
                    {
                        StopCoroutine(wait());
                        follow = true;
                        Debug.Log("ready to chase");
                        Debug.DrawLine(transform.position, hit.transform.position, Color.white, 2000000000000000.5f);
                    }    
                }
                
                
                
            }

            
            
           
            
           
        }
        else
        {
            if (check == true)
            {
                Debug.Log("yessssss");
                StartCoroutine(wait2());
            }
            if (check == false)
            {
                StopCoroutine(wait2());
            }
            nien = false;
            follow = false;
            timeup = false;
            
        }
        if (collis == false)
        {

            if (check == true)
            {

                Debug.Log("yessssss");
                StartCoroutine(wait2());
            }

            nien = false;
            temp = false;
            timeup = false;
        }

    }
    IEnumerator wait()
    {
        yield return new WaitForSeconds(waittime);
        Debug.Log("huh");
        if (nien == true)
        {
            timeup = true;
        }
        StopCoroutine(wait());

    }
    IEnumerator wait2()
    {
        Debug.Log("worksmabey?");
        follow = true;
        yield return new WaitForSeconds(chasetime);
        Debug.Log("huh");
        follow = false;
        agent.SetDestination(new Vector3(positionx, positiony, positionz));
        check = false;
        StopCoroutine(wait2());

    }

}


this script is in a kind of view box

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

public class detectCollision : MonoBehaviour
{
    
    private void Start()
    {
        GameObject theCam = GameObject.Find("enemyCamera");
        movetoplayer viewScript = theCam.GetComponent<movetoplayer>();
        viewScript.collis = false;
    }
    private void OnTriggerStay(Collider other)
    {
        GameObject theCam = GameObject.Find("enemyCamera");
        movetoplayer viewScript = theCam.GetComponent<movetoplayer>();
        viewScript.collis = true;
    }
    private void OnTriggerExit(Collider other)
    {
        GameObject theCam = GameObject.Find("enemyCamera");
        movetoplayer viewScript = theCam.GetComponent<movetoplayer>();
        viewScript.collis = false;
    }
}

Files

V4 49 MB
Jun 04, 2020

Get Duo Shot

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.