Search Unity

Enemy zombie AI

Discussion in 'Documentation' started by Cristian_1993, May 30, 2017.

?

Please help me fix the zombie AI

  1. How to fix the AI

    87.5%
  2. How to fix the collider

    50.0%
Multiple votes are allowed.
  1. Cristian_1993

    Cristian_1993

    Joined:
    Oct 2, 2015
    Posts:
    2
    I need help with the following script:

    -the zombies find random object with tag "Barricade" and lurk around it.
    -the go through any rigidbody and box collider with the tag "Barricade" that it s in their way. They simply ignore the colliders.
    -they don't always perform the attack function when they get in melee.

    I need help with:
    I want the zombies to find the objects with the tag "barricade" and attack them randomly when they get in melee range. Also i want them to record collisions.




    using UnityEngine;
    using System.Collections;

    public class AiFindTag : MonoBehaviour
    {

    public Transform target;
    public string enemyTag = "Barricade";
    float f_RotSpeed = 3.0f;
    float f_MoveSpeed = 3.0f;
    public float attackDist=2f;
    public float attackTimer =3f;
    public float cooldown;
    public int health = 150;
    public GameObject theCrate;

    void Start()
    {
    InvokeRepeating("UpdateTarget", 0f, 0.5f);
    cooldown = 2.0f;
    }

    public void takeDamage(int amount) {
    health -= amount;
    if (health <= 0)
    {
    Die();
    }
    }

    void Die() {
    Destroy(gameObject);
    if (Random.value > 0.9)
    {
    GameObject crateSpawn = (GameObject)Instantiate(theCrate, transform.position, transform.rotation);
    }
    }


    void UpdateTarget()
    {
    GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
    float shortestDistance = Mathf.Infinity;
    GameObject nearestEnemy = null;






    foreach (GameObject enemy in enemies)
    {
    float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
    if (distanceToEnemy < shortestDistance)
    {
    shortestDistance = distanceToEnemy;
    nearestEnemy = enemy;
    }
    }

    if (nearestEnemy != null)
    {
    // target = nearestEnemy.transform;
    target = enemies[Random.Range(0, enemies.Length)].transform;
    }
    else
    {
    // target = null;
    }


    }

    void Update()
    {


    if (target == null)
    return;

    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), f_RotSpeed * Time.deltaTime); ;
    //move to player


    transform.position += transform.forward * f_MoveSpeed * Time.deltaTime;

    if (attackTimer > 0)
    attackTimer -= Time.deltaTime;

    if (attackTimer < 0)
    attackTimer = 0;

    if (attackTimer == 0)
    {

    Attack();
    attackTimer = cooldown;
    }

    }


    private void Attack()
    {
    float distance = Vector3.Distance(target.transform.position, transform.position);


    Vector3 dir = (target.transform.position - transform.position).normalized;
    float direction = Vector3.Dot(dir, transform.forward);


    if (distance < attackDist)
    {
    if (direction > 0)
    {

    Debug.Log("Atacked");
    }
    }
    }

    }
     

    Attached Files:

  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287