- Home /
Detecting Collision Between Player and Enemy
Hey All,
I'm having some major problems with this one. I have a player bot that can run around the scene. It has a character controller used for moving it around and animating and works great. I have an enemy bot (also with a character controller for moving) that right now is just standing in the scene. I'm trying to implement a tag type game, so I want the player to be able to run up to the bot and "touch" it (have the colliders touch) and make whichever one of them is currently not it, it, and vice versa.
I have the function OnControllerColliderHit in the player controls, and with some debug calls can detect the ground, a wall I have in the scene, but NOT the enemy bot! I tried adding the same function to the enemy bot and it doesn't call anything. I thought maybe it would only work if both bots are moving when they collide, so I tried the function on a patrolling bot and ran into him, but no such luck.
Here's the code I'm using:
function OnControllerColliderHit (hit : ControllerColliderHit) {
if (hit.collider.gameObject.name != "Ground") {
Debug.Log("controller = " +hit.controller);
Debug.Log("gameObject = " + hit.gameObject);
Debug.Log("transform = " +hit.transform);
Debug.Log("hit: " +hit.collider.gameObject.name);
}
}
I'm just trying to see how to detect that enemy bot. I've been trying to work on this the last few days and it's killing my brain. I was able to use RayCasting to detect the other bot, but the problem (I was using a sphere ray cast), was that if I stood right on the boundary between the two bots, it would flip back and forth very quickly and it was more random whether or not the Player would come out it, or not it.
Any ideas guys?
Dan
Answer by ninjarob · Oct 30, 2011 at 09:02 PM
ok try this create a script called Tag
public var isIt:boolean = false;
function OnCollisionEnter(collision : Collision) {
var tag:Tag = collision.gameObject.GetComponent("Tag");
if(tag && isIt){
tag.isIt=true;
isIt=false;
}
}
You'll need to set one of the player to be It at the start and put the tag script on all players involved