Need help With Zombie Attacking Player Please!

Hi!
I CANNOT seem to get any help from anyone on this one,
I need the zombie to attack the player…
THAT simple… lol
It was a script from this video series and cannot contact him either:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Events;
using UnityStandardAssets.Characters;
using BNG;
public class AIExample : MonoBehaviour {
    public enum WanderType { Random, Waypoint};
 
    //public CharacterController fpsc;
    public WanderType wanderType = WanderType.Random;
    public float wanderSpeed = 4f;
    public float chaseSpeed = 7f;
    public float fov = 120f;
    public float viewDistance = 10f;
    public float wanderRadius = 7f;
    public Transform[] waypoints; //Array of waypoints is only used when waypoint wandering is selected
    //public Transform Player;
    private bool isAware = false;
    private Vector3 wanderPoint;
    private NavMeshAgent agent;
    private Renderer renderer;
    private int waypointIndex = 0;
    private Animator animator;
    public PlayerController player; 
    private SphereCollider sphereCollider;
    //    public GameObject CharacterController;
    //public Renderer renderer;
 
    //[Header("Events")]
    //[Tooltip("Optional Event to be called when receiving damage. Takes damage amount as a float parameter.")]
    //public FloatEvent onDamaged;
    [Tooltip("Optional Event to be called once health is <= 0")]
    public UnityEvent onDestroyed;
 
 
    public void Start()
    { 
        //fpsc = GetComponent<CharacterController>();
        agent = GetComponent<NavMeshAgent>();
        renderer = GetComponent<Renderer>();
        animator = GetComponent<Animator>();
        wanderPoint = RandomWanderPoint();
     
    }
    public void Update()
 
    {
        if (isAware)
     
        {
            agent.SetDestination(PlayerController.transform.position);
            animator.SetBool("Aware", true);
            agent.speed = chaseSpeed;
            renderer.material.color = Color.red;
            {
            if (isAware)
                isAware = true;
            transform.CompareTag("Player");}
         
             
            //if (Player){
            //    transform.CompareTag("Player");
             
            SearchForPlayer();
            Wander();
            animator.SetBool("Aware", true);
            agent.speed = wanderSpeed;
            renderer.material.color = Color.red;
            //}
         
        } else
        {
            SearchForPlayer();
            Wander();
            animator.SetBool("Aware", false);
            agent.speed = wanderSpeed;
            renderer.material.color = Color.blue;
        }
    }
 
    public void SearchForPlayer()
    {
        if (Vector3.Angle(Vector3.forward, transform.InverseTransformPoint(PlayerController.transform.position)) < fov / 0.7f)
        {
            if (Vector3.Distance(PlayerController.transform.position, transform.position) < viewDistance)
            {
                RaycastHit hit;
                if (Physics.Linecast(transform.position, PlayerController.transform.position, out hit, -1))
                {
                    if (hit.transform.CompareTag("Player"))
                    {
                        OnAware();
                    }
                }
            }
        }
    }
    public void OnAware()
    {
        isAware = true;
    }
    public void Wander()
    {
        if (wanderType == WanderType.Random)
        {
            if (Vector3.Distance(transform.position, wanderPoint) < 2f)
            {
                wanderPoint = RandomWanderPoint();
            }
            else
            {
                agent.SetDestination(wanderPoint);
            }
        }
        else
        {
            //Waypoint wandering
            if (waypoints.Length >= 2)
            {
                if (Vector3.Distance(waypoints[waypointIndex].position, transform.position) < 0.7f)
                {
                    if (waypointIndex == waypoints.Length - 1)
                    {
                        waypointIndex = 0;
                    }
                    else
                    {
                        waypointIndex++;
                    }
                }
                else
                {
                    agent.SetDestination(waypoints[waypointIndex].position);
                }
            } else
            {
                Debug.LogWarning("Please assign more than 1 waypoint to the AI: " + gameObject.name);
            }
        }
    }
    public Vector3 RandomWanderPoint()
    {
        Vector3 randomPoint = (Random.insideUnitSphere * wanderRadius) + transform.position;
        NavMeshHit navHit;
        NavMesh.SamplePosition(randomPoint, out navHit, wanderRadius, -1);
        return new Vector3(navHit.position.x, transform.position.y, navHit.position.z);
    }
 
    public void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            other.GetComponent<AIExample>().OnAware();
        }
    }
    private static void CharacterController();
    {
}

I also have the animations and everything setup but cannot for the life of me figure this out.??

He (the zombie) just kinda goes in circles swinging his arms!?

That’s incredibly simple! It’s also incredibly vague. What does “attacking the player” mean? Maybe the zombie follows the player and bites them? Maybe the zombie makes some mean social media posts? Maybe the zombie pulls out a rocket launcher and shoots at the player?

Even in the simple case of “following and biting”, there’s still a lot to unpack here. Are there animations involved? Do you need complex pathfinding?

3 Likes

LoL!
I LOVE your post!!! hehe
Well, just follow and bite me really… that’s all

Yes there are animations too involved reaching out to the player and he just goes in circles lmao

It’s also just plain NavMesh As Shown… =)

It looks like the “follow and attack the player” part is handled somewhere in part 1:

I’m trying to use it in my current Virtual Reality Project Too… :stuck_out_tongue_winking_eye:

Thanks I will look at that video that i somehow missed now!!!
=O

Thanks And I Will Give An Update Too!

for some reason i get this error though Assets\Scripts\PlayerExample.cs(54,5): error CS0106: The modifier ‘public’ is not valid for this item

should i make it private because it then shoots another error though???

You’re going to have to share the relevant code. If it’s the same as you shared above with no changes, then neither public nor private is appropriate on that line. You can only mark instance members public or private. Things like methods, fields, properties, and events that belong to the class. Code inside methods cannot have access modifiers like public and private.

1 Like

ok i am clear of Errors but the zombie just moves in place???

oh ok

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Events;
using UnityStandardAssets.Characters;
using BNG;

public class AIExample : MonoBehaviour {

    public enum WanderType { Random, Waypoint};

   
    //public CharacterController fpsc;
    public WanderType wanderType = WanderType.Random;
    public float wanderSpeed = 4f;
    public float chaseSpeed = 7f;
    public float fov = 120f;
    public float viewDistance = 10f;
    public float wanderRadius = 7f;
    public Transform[] waypoints; //Array of waypoints is only used when waypoint wandering is selected
    //public Transform Player;
    private bool isAware = false;
    private Vector3 wanderPoint;
    private NavMeshAgent agent;
    private Renderer renderer;
    private int waypointIndex = 0;
    private Animator animator;
    //public BNGPlayerController player;   
    private SphereCollider sphereCollider;
    //    public GameObject CharacterController;
    //public Renderer renderer;
    private BNGPlayerController fpsc;
    //[Header("Events")]
    //[Tooltip("Optional Event to be called when receiving damage. Takes damage amount as a float parameter.")]
    //public FloatEvent onDamaged;

    //[Tooltip("Optional Event to be called once health is <= 0")]
    //public UnityEvent onDestroyed;
   
   
    public void Start()
    {  
        fpsc = GetComponent<BNGPlayerController>();
        agent = GetComponent<NavMeshAgent>();
        renderer = GetComponent<Renderer>();
        animator = GetComponent<Animator>();
        wanderPoint = RandomWanderPoint();
       
    }
    public void Update()
   
    {
        if (isAware)
       
        {
            agent.SetDestination(fpsc.transform.position);
            animator.SetBool("Aware", true);
            agent.speed = chaseSpeed;
            renderer.material.color = Color.red;
            {
            if (isAware)
                isAware = true;
            transform.CompareTag("Player");}
          
               
            //if (Player){
            //    transform.CompareTag("Player");
               
            SearchForPlayer();
            Wander();
            animator.SetBool("Aware", true);
            agent.speed = wanderSpeed;
            renderer.material.color = Color.red;
            //}
           
        } else
        {
            SearchForPlayer();
            Wander();
            animator.SetBool("Aware", false);
            agent.speed = wanderSpeed;
            renderer.material.color = Color.blue;
        }
    }
   
    public void SearchForPlayer()
    {
        if (Vector3.Angle(Vector3.forward, transform.InverseTransformPoint(fpsc.transform.position)) < fov / 0.7f)
        {
            if (Vector3.Distance(fpsc.transform.position, transform.position) < viewDistance)
            {
                RaycastHit hit;
                if (Physics.Linecast(transform.position, fpsc.transform.position, out hit, -1))
                {
                    if (hit.transform.CompareTag("Player"))
                    {
                        OnAware();
                    }
                }
            }
        }
    }

    public void OnAware()
    {
        isAware = true;
    }

    public void Wander()
    {
        if (wanderType == WanderType.Random)
        {
            if (Vector3.Distance(transform.position, wanderPoint) < 2f)
            {
                wanderPoint = RandomWanderPoint();
            }
            else
            {
                agent.SetDestination(wanderPoint);
            }
        }
        else
        {
            //Waypoint wandering
            if (waypoints.Length >= 2)
            {
                if (Vector3.Distance(waypoints[waypointIndex].position, transform.position) < 0.7f)
                {
                    if (waypointIndex == waypoints.Length - 1)
                    {
                        waypointIndex = 0;
                    }
                    else
                    {
                        waypointIndex++;
                    }
                }
                else
                {
                    agent.SetDestination(waypoints[waypointIndex].position);
                }
            } else
            {
                Debug.LogWarning("Please assign more than 1 waypoint to the AI: " + gameObject.name);
            }
        }
    }

    public Vector3 RandomWanderPoint()
    {
        Vector3 randomPoint = (Random.insideUnitSphere * wanderRadius) + transform.position;
        NavMeshHit navHit;
        NavMesh.SamplePosition(randomPoint, out navHit, wanderRadius, -1);
        return new Vector3(navHit.position.x, transform.position.y, navHit.position.z);
    }
   
    public void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            other.GetComponent<AIExample>().OnAware();
        }
    }

    //private static void CharacterController();

   
}

Still need some help on this please.

bump…

bump… again

FFS B.U.M.P.!!!
B.ring
U.nderstanding
M.y Way
Pl.ease!

You’re being very cryptic as to what problem you’re trying to solve, when you need to be extremely specific or no one can help. No idea what “the zombie just moves in place” even means.

Aren’t “moves” and “in place” opposites? Are you talking about moving slowly? Are you talking about animation with no actual movement? Are you talking about rotation? You’ve got to be extremely clear what the issue is.

One thing I notice though is you aren’t using Debug.Log. You should be putting it everywhere you are investigating to see what code path is actually occurring, which may be different than you are expecting.

2 Likes

moves in place…

to simplify for everyone i meant moves in the same transform position thus in the same place as to where the zombie is…

I’m not being cryptic at all…
My zombie “moves” but in the same position he is in i cannot get any clearer than that my friend…

bump,
I need the zombie to attack the player…
THAT simple… lol

anyone please? =)