Search Unity

Question Enemy can't activate a trigger placed on a mesh attached to the player.

Discussion in 'Getting Started' started by d33unity, Jan 6, 2023.

  1. d33unity

    d33unity

    Joined:
    Oct 22, 2022
    Posts:
    14
    Hi all, I really can use some help with this part. I am following this tutorial and in about 4:07
    where the instructor triggers the zombie enemy to attack the player via a trigger placed on a cylinder mesh attached to the player, the trigger is just not working.


    Here is the Zombie AI script:

    Code (CSharp):
    1.    
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ZombieAI : MonoBehaviour
    7. {
    8.     public GameObject ThePlayer;
    9.     public GameObject TheEnemy;
    10.     public float EnemySpeed = 0.01f;
    11.     public bool AttackTrigger = false;
    12.     public bool IsAttacking = false;
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         transform.LookAt(ThePlayer.transform);
    18.         if(AttackTrigger == false)
    19.         {
    20.             EnemySpeed = 0.01f;
    21.             TheEnemy.GetComponent<Animation>().Play("Z_Walk 1");
    22.             transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position,EnemySpeed);
    23.         }
    24.  
    25.         if(AttackTrigger == true && IsAttacking == false)
    26.         {
    27.             EnemySpeed = 0;
    28.             TheEnemy.GetComponent<Animation>().Play("Z_Attack 1");
    29.             StartCoroutine(InflictDamage());
    30.         }
    31.     }
    32.  
    33.     void OnTriggerEnter()
    34.     {
    35.         AttackTrigger = true;
    36.     }
    37.  
    38.  
    39.  
    40.     void OnTriggerExit()
    41.     {
    42.         AttackTrigger = false;
    43.     }
    44.  
    45.     IEnumerator InflictDamage()
    46.     {
    47.         IsAttacking = true;
    48.         yield return new WaitForSeconds(1.1f);
    49.         GlobalHealth.CurrentHealth -= 5;
    50.         yield return new WaitForSeconds(0.2f);
    51.         IsAttacking = false;
    52.        
    53.     }
    54. }
    55.  
    56. }
    One more thing, I use a different asset package for the player, called starter assets. What the instructor used in his tutorial does not exist any more in the asset store. Anyway, I even tried placing a mesh collider or a box collider, set to trigger, on other parts of the player such as "PlayerCapsule" or "PlayerFollowCamera" but to no avail.

    The zombie has a box collider not set as a trigger. Can anybody help me with this problem? Many thanks for reading my post!
     
  2. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Is trigger collider radius set bigger than actual player collider? Maybe it is smaller and zombie never reaches it
     
  3. d33unity

    d33unity

    Joined:
    Oct 22, 2022
    Posts:
    14
    Hi, thank you for your reply to my post. Yes I did tried setting a bigger collider radius to both the zombie and the player and they didn't collide.
     
  4. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Try to specify the trigger collider in OnTriggerEnter. First, try just OnTriggerEnter(Collider coll), second, try feeding it reference to to a specific collider
    Code (CSharp):
    1. Collider PlayerTrigger = GameObject.Find("TriggerObjectName").collider;
    2. OnTriggerEnter(PlayerTrigger){
    3. ...
    4. }
    5.  
    Not guaranteed to work, but I remember there was usually something put into OnTriggerEnter() braces
     
  5. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Also, the manual says that trigger needs to have Rigidbody component
     
  6. d33unity

    d33unity

    Joined:
    Oct 22, 2022
    Posts:
    14
    Hi, thank you again for your reply to my post. I tried again adding the rigidbody component to the zombie, set it to kinetic, and it did worked this time. One thing I didn't understand from the tutorial is that the instructor deletes the rigidbody component from the zombie (It made the zombie at first to disappear) and it still worked for him - the zombie did collide with the player...
     
  7. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Do not know. Maybe the rigidbody was on player's trigger volume, but you'd notice it then. Manual says that at least one object needs to have a rigidbody
     
  8. d33unity

    d33unity

    Joined:
    Oct 22, 2022
    Posts:
    14
    I used a different assets package for the player(called starter assets). The assets the instructor uses for the player is no longer available at the store. Maybe that's what that caused all that strange behavior.
     
  9. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    And that right there is the problem with most tute projects, the projects are at least 3 years out of date, and don't work.
     
  10. d33unity

    d33unity

    Joined:
    Oct 22, 2022
    Posts:
    14
    I hope I could somehow manage to keep up with this tutorial. It just that it seemed very interesting to me when I had searched for tutorials to start my journey in Unity and the end result is something that I aspire to achieve. So far, I learnt a lot from this tutorial. It is a 12 hours tutorial. So I don't know... hope it will work out at the end...
     
  11. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    Good luck, I hope you achieve something.
     
    d33unity likes this.
  12. d33unity

    d33unity

    Joined:
    Oct 22, 2022
    Posts:
    14
    Thank you! Good luck to you as well!