Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How do I add shooting enemy AI in Unity 3d?

JeetJeet Member
edited June 2020 in Programming

Hi I am new to unity and if you know how to add shooting enemy AI in unity 3d please help me out!!!

Any Help will be appreciated!!

Thanks

Best Answer

  • Sugar_SnailSugar_Snail Member
    Accepted Answer


    in the update function you can do

    Collider collide = Physics.OverlapSphere(enemy.position, sphereSize, whatIsPlayer);

    if(collide = null) //if the player isnt in it's 'bubble'

    {

    InvokeRepeating("Shoot", 2f, shootRate);

    transform.LookAt(player.position, vector3.up);

    } else

    {

    CancelInvoke();

    transform.LookAt(-player.position, vector3.up);

    }


    in the else you can make it run away

Answers

  • JeetJeet Member

    @Sugar_Snail  Thanks a lot!!

  • what , can you make the full script ?

  • JeetJeet Member
    edited August 2020

    @nio2edward

    I made the whole thing on my own

    For the shooting enemy AI use this script

    BTW the player Cube GameObject here is just a cube as child of player without mesh renderer & collider to reduce shot accuracy

    if you want 100% shot accuracy just drag your player into that

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;
    
    public class EnemyAI : MonoBehaviour
    {
    public float LookDistance;
    public float LOD;
    
    public float FireRate = 15f;
    public float ReloadTime = 2f;
    
    [SerializeField]
    private float NextTimeToFire = 2f;
    
    [SerializeField]
    private int maxEnemyAmmo = 50;
    
    [SerializeField]
    private int currentEnemyAmmo;
    
    [SerializeField]
    private int BulletsPerShot;
    
    [SerializeField]
    private bool isReloading;
    
    public GameObject PlayerCube;
    
    public Transform playerTrans; 
    public Transform GunTip;
    
    public Animator enemyAnim;
    
    
    public MyPlayerHealth playerHealth;
    
    NavMeshAgent enemy;
    
    public AudioSource EnemyShootSound;
    
    public Cast cast;
    
    void Start()
    {
    currentEnemyAmmo = maxEnemyAmmo;
    
    enemy = this.GetComponent<NavMeshAgent>();
    
    MyPlayerHealth playerHealth = gameObject.GetComponent<MyPlayerHealth>();
    
    Cast cast = gameObject.GetComponent<Cast>();
    
    }
    
    void OnEnable()
    {
    isReloading = false;
    enemyAnim.SetBool("Reloading", false);
    }
    
    public void Update()
    
    {
    if (isReloading)
    return;
    
    if (currentEnemyAmmo <= 0)
    {
    StartCoroutine(reloding());
    return;
    }
    
    
    
    
    
    
    
    
    
    IEnumerator reloding()
    {
    isReloading = true;
    
    enemyAnim.SetBool("Reloading", true);
    
    yield return new WaitForSeconds(ReloadTime - 0.25f);
    
    enemyAnim.SetBool("Reloading", false);
    
    yield return new WaitForSeconds(0.25f);
    
    currentEnemyAmmo = maxEnemyAmmo;
    
    isReloading = false;
    }
    
    
    }
    
    public void LookAtPlayer()
      {
    
    Vector3 direction = playerTrans.transform.position - this.transform.position;//Look at player
    
    float angle = Vector3.Angle(direction, this.transform.forward);
    
    if (Vector3.Distance(playerTrans.position, this.transform.position) < LookDistance)
    {
    
    
    direction.y = 0;
    this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
    cast.Do();
    }
    }
    }
    
    
    
    
  • JeetJeet Member

    Attach this script above on your my and add an empty gameObject where you want your enemy to shoot from (GunTip in this script) Ad the script bellow to your empty gameObject

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Cast : MonoBehaviour
    {
      public MyPlayerHealth PH;
      public float shootDist;
      public EnemyAI EA;
    
       void Start()
      {
        PH = gameObject.GetComponent<MyPlayerHealth>();
    
        EA = gameObject.GetComponent<EnemyAI>();
      }
    
      public void Do()
      {
        RaycastHit hit;
        Ray ray = new Ray(transform.position, transform.TransformDirection(Vector3.forward));
    
        Debug.DrawRay(transform.position, Vector3.forward * shootDist);
    
        if (Physics.Raycast(ray, out hit, Mathf.Infinity))
        {
          if(hit.collider.name == "Player")
          {
            PH.damage();
            EA.LookAtPlayer();
          }
           
        }
    
      }
    }
    


    Ad the script below to your player


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MyPlayerHealth : MonoBehaviour
    {
    public int MaxHealth = 100;
    public int CurrentHealth;
    public int Damage = 1;
    
    private void Start()
    {
    CurrentHealth = MaxHealth;
    }
    
      public void damage()
    {
    if(CurrentHealth >= 0)
        {
    CurrentHealth = CurrentHealth - Damage;
    }
    if (CurrentHealth == 0)
    {
    Debug.Log("Dead");
    }
    }
    }
    


Sign In or Register to comment.