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();
{
}
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?
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.
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.