Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help With Zombie Attacking Player Please!

Discussion in 'Scripting' started by metaldc4life, Sep 2, 2020.

  1. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    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:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using UnityEngine.Events;
    6. using UnityStandardAssets.Characters;
    7. using BNG;
    8. public class AIExample : MonoBehaviour {
    9.     public enum WanderType { Random, Waypoint};
    10.  
    11.     //public CharacterController fpsc;
    12.     public WanderType wanderType = WanderType.Random;
    13.     public float wanderSpeed = 4f;
    14.     public float chaseSpeed = 7f;
    15.     public float fov = 120f;
    16.     public float viewDistance = 10f;
    17.     public float wanderRadius = 7f;
    18.     public Transform[] waypoints; //Array of waypoints is only used when waypoint wandering is selected
    19.     //public Transform Player;
    20.     private bool isAware = false;
    21.     private Vector3 wanderPoint;
    22.     private NavMeshAgent agent;
    23.     private Renderer renderer;
    24.     private int waypointIndex = 0;
    25.     private Animator animator;
    26.     public PlayerController player;
    27.     private SphereCollider sphereCollider;
    28.     //    public GameObject CharacterController;
    29.     //public Renderer renderer;
    30.  
    31.     //[Header("Events")]
    32.     //[Tooltip("Optional Event to be called when receiving damage. Takes damage amount as a float parameter.")]
    33.     //public FloatEvent onDamaged;
    34.     [Tooltip("Optional Event to be called once health is <= 0")]
    35.     public UnityEvent onDestroyed;
    36.  
    37.  
    38.     public void Start()
    39.     {
    40.         //fpsc = GetComponent<CharacterController>();
    41.         agent = GetComponent<NavMeshAgent>();
    42.         renderer = GetComponent<Renderer>();
    43.         animator = GetComponent<Animator>();
    44.         wanderPoint = RandomWanderPoint();
    45.      
    46.     }
    47.     public void Update()
    48.  
    49.     {
    50.         if (isAware)
    51.      
    52.         {
    53.             agent.SetDestination(PlayerController.transform.position);
    54.             animator.SetBool("Aware", true);
    55.             agent.speed = chaseSpeed;
    56.             renderer.material.color = Color.red;
    57.             {
    58.             if (isAware)
    59.                 isAware = true;
    60.             transform.CompareTag("Player");}
    61.          
    62.              
    63.             //if (Player){
    64.             //    transform.CompareTag("Player");
    65.              
    66.             SearchForPlayer();
    67.             Wander();
    68.             animator.SetBool("Aware", true);
    69.             agent.speed = wanderSpeed;
    70.             renderer.material.color = Color.red;
    71.             //}
    72.          
    73.         } else
    74.         {
    75.             SearchForPlayer();
    76.             Wander();
    77.             animator.SetBool("Aware", false);
    78.             agent.speed = wanderSpeed;
    79.             renderer.material.color = Color.blue;
    80.         }
    81.     }
    82.  
    83.     public void SearchForPlayer()
    84.     {
    85.         if (Vector3.Angle(Vector3.forward, transform.InverseTransformPoint(PlayerController.transform.position)) < fov / 0.7f)
    86.         {
    87.             if (Vector3.Distance(PlayerController.transform.position, transform.position) < viewDistance)
    88.             {
    89.                 RaycastHit hit;
    90.                 if (Physics.Linecast(transform.position, PlayerController.transform.position, out hit, -1))
    91.                 {
    92.                     if (hit.transform.CompareTag("Player"))
    93.                     {
    94.                         OnAware();
    95.                     }
    96.                 }
    97.             }
    98.         }
    99.     }
    100.     public void OnAware()
    101.     {
    102.         isAware = true;
    103.     }
    104.     public void Wander()
    105.     {
    106.         if (wanderType == WanderType.Random)
    107.         {
    108.             if (Vector3.Distance(transform.position, wanderPoint) < 2f)
    109.             {
    110.                 wanderPoint = RandomWanderPoint();
    111.             }
    112.             else
    113.             {
    114.                 agent.SetDestination(wanderPoint);
    115.             }
    116.         }
    117.         else
    118.         {
    119.             //Waypoint wandering
    120.             if (waypoints.Length >= 2)
    121.             {
    122.                 if (Vector3.Distance(waypoints[waypointIndex].position, transform.position) < 0.7f)
    123.                 {
    124.                     if (waypointIndex == waypoints.Length - 1)
    125.                     {
    126.                         waypointIndex = 0;
    127.                     }
    128.                     else
    129.                     {
    130.                         waypointIndex++;
    131.                     }
    132.                 }
    133.                 else
    134.                 {
    135.                     agent.SetDestination(waypoints[waypointIndex].position);
    136.                 }
    137.             } else
    138.             {
    139.                 Debug.LogWarning("Please assign more than 1 waypoint to the AI: " + gameObject.name);
    140.             }
    141.         }
    142.     }
    143.     public Vector3 RandomWanderPoint()
    144.     {
    145.         Vector3 randomPoint = (Random.insideUnitSphere * wanderRadius) + transform.position;
    146.         NavMeshHit navHit;
    147.         NavMesh.SamplePosition(randomPoint, out navHit, wanderRadius, -1);
    148.         return new Vector3(navHit.position.x, transform.position.y, navHit.position.z);
    149.     }
    150.  
    151.     public void OnTriggerEnter(Collider other)
    152.     {
    153.         if (other.gameObject.CompareTag("Player"))
    154.         {
    155.             other.GetComponent<AIExample>().OnAware();
    156.         }
    157.     }
    158.     private static void CharacterController();
    159.     {
    160. }
     
    Last edited: Sep 2, 2020
  2. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    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!?
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    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?
     
  4. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    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
     
  5. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179

    It's also just plain NavMesh As Shown.. =)
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    It looks like the "follow and attack the player" part is handled somewhere in part 1:

     
  7. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179

    I'm trying to use it in my current Virtual Reality Project Too.. ;P
     
  8. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    Thanks I will look at that video that i somehow missed now!!!
    =O

    Thanks And I Will Give An Update Too!
     
  9. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    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???
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    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.
     
    metaldc4life likes this.
  11. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    ok i am clear of Errors but the zombie just moves in place???

    oh ok
     
  12. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using UnityEngine.Events;
    6. using UnityStandardAssets.Characters;
    7. using BNG;
    8.  
    9. public class AIExample : MonoBehaviour {
    10.  
    11.     public enum WanderType { Random, Waypoint};
    12.  
    13.    
    14.     //public CharacterController fpsc;
    15.     public WanderType wanderType = WanderType.Random;
    16.     public float wanderSpeed = 4f;
    17.     public float chaseSpeed = 7f;
    18.     public float fov = 120f;
    19.     public float viewDistance = 10f;
    20.     public float wanderRadius = 7f;
    21.     public Transform[] waypoints; //Array of waypoints is only used when waypoint wandering is selected
    22.     //public Transform Player;
    23.     private bool isAware = false;
    24.     private Vector3 wanderPoint;
    25.     private NavMeshAgent agent;
    26.     private Renderer renderer;
    27.     private int waypointIndex = 0;
    28.     private Animator animator;
    29.     //public BNGPlayerController player;  
    30.     private SphereCollider sphereCollider;
    31.     //    public GameObject CharacterController;
    32.     //public Renderer renderer;
    33.     private BNGPlayerController fpsc;
    34.     //[Header("Events")]
    35.     //[Tooltip("Optional Event to be called when receiving damage. Takes damage amount as a float parameter.")]
    36.     //public FloatEvent onDamaged;
    37.  
    38.     //[Tooltip("Optional Event to be called once health is <= 0")]
    39.     //public UnityEvent onDestroyed;
    40.    
    41.    
    42.     public void Start()
    43.     {  
    44.         fpsc = GetComponent<BNGPlayerController>();
    45.         agent = GetComponent<NavMeshAgent>();
    46.         renderer = GetComponent<Renderer>();
    47.         animator = GetComponent<Animator>();
    48.         wanderPoint = RandomWanderPoint();
    49.        
    50.     }
    51.     public void Update()
    52.    
    53.     {
    54.         if (isAware)
    55.        
    56.         {
    57.             agent.SetDestination(fpsc.transform.position);
    58.             animator.SetBool("Aware", true);
    59.             agent.speed = chaseSpeed;
    60.             renderer.material.color = Color.red;
    61.             {
    62.             if (isAware)
    63.                 isAware = true;
    64.             transform.CompareTag("Player");}
    65.          
    66.                
    67.             //if (Player){
    68.             //    transform.CompareTag("Player");
    69.                
    70.             SearchForPlayer();
    71.             Wander();
    72.             animator.SetBool("Aware", true);
    73.             agent.speed = wanderSpeed;
    74.             renderer.material.color = Color.red;
    75.             //}
    76.            
    77.         } else
    78.         {
    79.             SearchForPlayer();
    80.             Wander();
    81.             animator.SetBool("Aware", false);
    82.             agent.speed = wanderSpeed;
    83.             renderer.material.color = Color.blue;
    84.         }
    85.     }
    86.    
    87.     public void SearchForPlayer()
    88.     {
    89.         if (Vector3.Angle(Vector3.forward, transform.InverseTransformPoint(fpsc.transform.position)) < fov / 0.7f)
    90.         {
    91.             if (Vector3.Distance(fpsc.transform.position, transform.position) < viewDistance)
    92.             {
    93.                 RaycastHit hit;
    94.                 if (Physics.Linecast(transform.position, fpsc.transform.position, out hit, -1))
    95.                 {
    96.                     if (hit.transform.CompareTag("Player"))
    97.                     {
    98.                         OnAware();
    99.                     }
    100.                 }
    101.             }
    102.         }
    103.     }
    104.  
    105.     public void OnAware()
    106.     {
    107.         isAware = true;
    108.     }
    109.  
    110.     public void Wander()
    111.     {
    112.         if (wanderType == WanderType.Random)
    113.         {
    114.             if (Vector3.Distance(transform.position, wanderPoint) < 2f)
    115.             {
    116.                 wanderPoint = RandomWanderPoint();
    117.             }
    118.             else
    119.             {
    120.                 agent.SetDestination(wanderPoint);
    121.             }
    122.         }
    123.         else
    124.         {
    125.             //Waypoint wandering
    126.             if (waypoints.Length >= 2)
    127.             {
    128.                 if (Vector3.Distance(waypoints[waypointIndex].position, transform.position) < 0.7f)
    129.                 {
    130.                     if (waypointIndex == waypoints.Length - 1)
    131.                     {
    132.                         waypointIndex = 0;
    133.                     }
    134.                     else
    135.                     {
    136.                         waypointIndex++;
    137.                     }
    138.                 }
    139.                 else
    140.                 {
    141.                     agent.SetDestination(waypoints[waypointIndex].position);
    142.                 }
    143.             } else
    144.             {
    145.                 Debug.LogWarning("Please assign more than 1 waypoint to the AI: " + gameObject.name);
    146.             }
    147.         }
    148.     }
    149.  
    150.     public Vector3 RandomWanderPoint()
    151.     {
    152.         Vector3 randomPoint = (Random.insideUnitSphere * wanderRadius) + transform.position;
    153.         NavMeshHit navHit;
    154.         NavMesh.SamplePosition(randomPoint, out navHit, wanderRadius, -1);
    155.         return new Vector3(navHit.position.x, transform.position.y, navHit.position.z);
    156.     }
    157.    
    158.     public void OnTriggerEnter(Collider other)
    159.     {
    160.         if (other.gameObject.CompareTag("Player"))
    161.         {
    162.             other.GetComponent<AIExample>().OnAware();
    163.         }
    164.     }
    165.  
    166.     //private static void CharacterController();
    167.  
    168.    
    169. }
    170.  
    171.    
    172.  
     
  13. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    Still need some help on this please.
     
  14. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
  15. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    bump... again
     
  16. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    FFS B.U.M.P.!!!!!!
    B.ring
    U.nderstanding
    M.y Way
    Pl.ease!
     
  17. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
    matkoniecz and Lethn like this.
  18. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    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..
     
  19. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    bump,
    I need the zombie to attack the player...
    THAT simple... lol
     
  20. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    anyone please? =)
     
  21. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    "Moves" and "same position" are like night and day, white an black, as in they are opposites which cannot both be true. I'm going to assume by "moves" you mean "does not at all in any way move whatsoever" though. Have you gone through all the steps necessary to set up the navmesh properly? If so, can you show an image of the navmesh in the scene view?

    https://docs.unity3d.com/Manual/nav-BuildingNavMesh.html
     
    matkoniecz and metaldc4life like this.
  22. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    Sir, you obviously want to get into a semantic argument with me and totally miss the point of this discussion that i originally made...
    I am not trying to be mean and i am sure you are not either but you are drifting each other from the true point....
    So let us not focus on that and the actual situation i have on hand please...

    With all of that said...


    the enemy zombie has motion in place (i guess to explain it that would mean he moves his arms but does not walk forward)
    do you know what i mean now?

    If you do would that mean i would need to change the transform of the zombie??


    Thank you and stay safe ok?

    P.S.and yes, i do know how to build a nav mesh ;)
     
    Last edited: Nov 17, 2020
  23. vertig02

    vertig02

    Joined:
    Nov 30, 2017
    Posts:
    61
    Resident Evil 3 flashbacks intensify
     
    metaldc4life likes this.
  24. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    lol yeah i thought that was good too

    but i meant just reaching out and trying to bite me HaHa..
     
  25. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Not trying to get into a semantic argument, just tried to understand what specifically you are talking about with "moves" for something which isn't changing position. The arms moving sounds like you're just talking about the animator. Got it. If the zombie was rotating towards the target but not moving forward for example, that would point to an entirely different problem, which is why if you want help it is important to be clear.

    I'd look at the code you posted on Sept 2. The Update method lines 61 through 75 doesn't look right. On line 57 you are setting the agent to go to the player position, but then on line 71 you call Wander() which is supposed to tell the agent to go to another location (which means it will stop chasing the player). You should only call Wander() if you're not chasing the player. You've also got what looks like another problem in Wander() around line 116 where when you generate a new wander point you don't actually tell the agent to go to that point. It looks like you could get into a loop of continually generating new wander points but never actually going to any of them. Not sure how that exactly plays out without some debugging added.

    But it doesn't sound like you're talking my questions or attempts at help seriously, so I'll just leave this at that. Good luck.
     
  26. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    Thank you for your help.. really i take it seriously i promise.. sorry about that!

    =/

    Thank you for all your input in this mater!

    Stay Safe Too..
     
    Joe-Censored likes this.
  27. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179

    It makes since now that you have pointed them out.. Thanks! ;)
     
    Joe-Censored likes this.