- Home /
Why none of the events OnTriggerEnter or OnCollisionEnter not fire ?
The script is attached to a GameObject a cylinder.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAttack : MonoBehaviour {
public bool entered = false;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Enemy")
entered = true;
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Enemy")
entered = true;
}
private void OnCollisionExit(Collision collision)
{
entered = false;
}
}
"Enemy" is the GameObject and that should be detected once it's entering the GameObject this script is attached to.
Entering or Collide with. But it doesn't matter none of the events work. I used a breakpoint and it's never get any of the events.
Answer by moltow · Aug 06, 2017 at 08:39 PM
Does this cylinder or the Enemy GameObject have a RigidBody attached?
Note from OnTriggerEnter docs:
Trigger events are only sent if one of the colliders also has a rigidbody attached.
Note from OnCollisionEnter docs:
Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.
Hope this helps.
I added to the Cylinder a Rigidbody. Only the Use Gravity is checked.
But then the Cylinder start to rotate and fly in the air and move away. The only way he stay on ground is if i check also the Is $$anonymous$$inematic. And when the Is kinematic is checked none of the events work.
You can use the restraints on the RigidBody attached to the Cylinder to prevent it from moving on you.
It's working once i checked also the Cylinder Capsule Collider component Is Trigger. so now in the rigidsbody both Use Gravity and Is $$anonymous$$inematic are checked ! And also the Collider Is Trigger is checked only on the Cylinder.
Now when the enemy collide with the Cylinder it's working it stop there using a break point.
Is this the behavior you want? If you use the Cylinder's Capsule Collider as a Trigger, your enemies will be able to run right through the cylinder, right?
Three things i forgot to mention.
First the cylinder i changed it's scale to be like a circle and this is only for setting a range where the enemy enter and leave. I have another gameobject that is a turret and stay in the center of the cylinder. This is all for a tower defence game.
it's only for testing so the enemies will have a waypoints route so they will never get too close to touch or pass through the cylinder/s.
Now i'm using my waypoints script so the enemy is getting close to the cylinder but then move back to the other side since i did that in distance of 30 before the cylinder change movement direction.
So i'm not sure if this is so important if i use the Capsule Collider trigger. What do you think ? If i'm doing it all for a tower defence game.
This is image showing the turret in the middle of the clyinder and the cube on the left is the enemy.
So the event ontriggerenter will be to tell the turret when to start shooting on the enemy. And ontriggerexit when to stop shooting. This cylinder is just to set the shooting range.
What do you think i mean by logic ?
Your answer
Follow this Question
Related Questions
How can i make an entrance and exit in this maze ? 1 Answer
How can I animate linerenderer lines over time ? 1 Answer
How can i find a thirdpersoncontroller hand and attach object to the hand ? 1 Answer
How can i check/wait until the gameobject will end the rotation ? 1 Answer
How can I call the Load method and/or the ShootingSettings method also only once in the Update ? 1 Answer