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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZombieAI : MonoBehaviour
{
public GameObject ThePlayer;
public GameObject TheEnemy;
public float EnemySpeed = 0.01f;
public bool AttackTrigger = false;
public bool IsAttacking = false;
// Update is called once per frame
void Update()
{
transform.LookAt(ThePlayer.transform);
if(AttackTrigger == false)
{
EnemySpeed = 0.01f;
TheEnemy.GetComponent<Animation>().Play("Z_Walk 1");
transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position,EnemySpeed);
}
if(AttackTrigger == true && IsAttacking == false)
{
EnemySpeed = 0;
TheEnemy.GetComponent<Animation>().Play("Z_Attack 1");
StartCoroutine(InflictDamage());
}
}
void OnTriggerEnter()
{
AttackTrigger = true;
}
void OnTriggerExit()
{
AttackTrigger = false;
}
IEnumerator InflictDamage()
{
IsAttacking = true;
yield return new WaitForSeconds(1.1f);
GlobalHealth.CurrentHealth -= 5;
yield return new WaitForSeconds(0.2f);
IsAttacking = false;
}
}
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?
-What I want:
I want that when the zombie collide with the player, a trigger mesh collider (a cylinder mesh attached to the player), will activate OnTriggerEnter and an “attack” animation will start playing as can be seen by the instructor’ script “zombie AI” above.
What I tried:
I tried to set to a large radius the mesh cylinder attached to the player (with the trigger mesh collider) but zombie still don’t collide and trigger the function OnTriggerEnter.
I also tried attach a mesh collider and a box collider to different parts of the player such as “PlayerCapsule” or “PlayerFollowCamera” (set to trigger) but to no avail.
What I expected to happen:
Trigger of the function OnTriggerEnter() by the collide of the zombie and the player;
What actually happened, especially any errors:
The zombie does nothing but continue walking. No set of any trigger and the playing of an attack animation. It didn’t report any errors.
- links to documentation you used to cross-check your work (CRITICAL!!!)
Did you review all the docs related to meeting ALL the requirements to receive trigger messages?
Did you name the functions correctly?
Are you moving the Rigidbody(ies) correctly?
With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.
Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.
How to report your problem productively in the Unity3D forums:
Hi, thank you very much for your detailed post. I edited my first post as you requested, adding more details.
Answers to your questions: Did you review all the docs related to meeting ALL the requirements to receive trigger messages?
In**:** https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
It said that in order to activate this function, both objects needs a collider and a rigidbody (When one of the colliders is set to trigger). So I did try the above and added a rigid body to the zombie and it did worked and the attack animation starts to play, but my player didn’t have any rigidbody attached to it and it still worked, so I don’t understand how it worked at that stage.
Problem is the instructor deletes the rigid body component he added to the zombie in that point of the tutorial, and I try to follow his tutorial as much as I can, so I don’t understand how in his case, after he deleted the rigidbody component, the zombie did collide with the player.
Did you name the functions correctly?
Yes. Are you moving the Rigidbody(ies) correctly?
As, I wrote, he deleted in that point of the tutorial the rigidbody he added to the zombie.
“With Physics (or Physics2D), never manipulate the Transform directly.”
I try to do exactly what the instructor does, so I don’t know how to change his script according to what you mentioned.