Easy Collision Problem

Hi, i want to establish an enemy, that walks towards the player and attacks, if he is close enough. Therefore i gave him an Empty GameObject with a Box Collider(Trigger=true) and a Rigidbody(Kinematic=true) as child.

This Empty GameObject also has a script Component “AI”, that shall detect a collision with the player. I already tested everything, so its meaningless to post it.

Please tell me how you wold manage this and if your approach works, thanks!

Hi, welcome to the forum!

Can you explain a bit further what is going on in the game? Is it an empty surface where the enemies just have to move toward the player or do they need to move around obstacles or… what?

andeeee asked the right question. If what you need is path finding like enemies chasing the player and behaviour choises like do something when you find him than you should check angryant.com they have what you need. They are both realy complex AI issues.

Im sorry, that I discribed my problem so unprecisely. The enemy behavior shall be very simple. He consists of the following attention states:

IDLE: The enemy doesn’t see the player and walks around randomly.

ATTACK: The enemy saw the player (the player touched his area of sight, that is the collider attached to him) and is walking towards him to attack him until he dies.

My problem is not the implementation of the attention states. Rather is my problem the point when the enemy shall switch between IDLE and ATTACK. If the player walks inside the enemys area of sight, there is no collision event triggered. The colliding objects are:

“Enemy sight”
Box Collider → isTrigger=1
Rigidbody → isKinematic=1

“Player”
Character Controller

I attached a script called “AI” to the “Enemy Sight” Object, that has the following code:

function OnCollisionEnter(collision : Collision){
	Debug.Log("sight touched "+ collision.gameObject.name);
}

If I test this the debug.log always says “sight touched Terrain” and never “sight touched Player”. If you know how I can detect a collision between “Enemy Sight” and “Player” please tell me.

If your Sight collider is a Trigger, it should be using

function OnTriggerEnter ( other : Collider ) {
 Debug.Log("Sight touched " + other.name );
}

I’m surprised its CollisionEnter function is being called at all, honestly.

Ok I changed it to

function OnTriggerEnter(other : Collider){
	Debug.Log("sight touched "+ other.name);
}

but it doesn’t work. Now the debug doesn’t print anything. Frustrating.

Isn’t it possible to let an empty object, which is a child of an object, detect a collision with a Character Controller?

I got it to detect the player now by creating a new cube and attaching it to the enemy and then asking for

OnTriggerEnter{}

but if I move the enemy by the use of velocity the collision doesn’t work anymore. It occurs, that the collider actually stays at its original position, because its still triggering a collision there. How can I make the collider always work?

Problem solved. Finally! The clue is that if you make a collider a child of a GameObject, that has a rigidbody, this child object mustn’t contain a rigidbody for itself. But thanks for you help!