- Home /
Collision issue with player and enemy
Hey guys, I am having a bit of an issue here.
What I have setup is a first person controller, and many enemies that follows the first person controller. I have this script on the first person controller:
var health = 4;
var damage = 1;
var wait_time = 2;
var lock = 0;
function OnCollisionEnter(hit: Collision){
if(hit.gameObject.tag == "enemy" && lock == 0){
StartCoroutine("DecreasingHealth");
}
}
function DecreasingHealth(){
lock=1;
health=health-damage;
yield WaitForSeconds(wait_time);
lock=0;
}
function Update(){
if(health == 0){
Destroy(gameObject);
}
}
Except....., the collision won't work. On the first person controller, I have the character controller which automatically has a collider on it and I have a non kinematic rididbody. I have the same non kinematic rididbody on the enemies, but I have added a capsule collider which has "Is Trigger" uncheckedd. There are also no errors for this script in the console....
Can anyone see what's wrong here???
Thanks!
-Grady
It sounds like the enemy has got to be directly touching the player, perhaps try adding a box collider (should be a bit bigger than player) and setting it to a trigger. Then check for OnTriggerEnter ins$$anonymous$$d of OnCollisionEnter, this could perhaps solve the problem. If it doesn't then perhaps try checking whether the collision is being detected through using Debug.Log(); I guess the problem could be that the two capsule colliders aren't colliding properly, so my suggestion would be to try adding the box collider and make sure that the enemy is within range of the collider.
It won't work for some reason, I keep getting this error:
Actor::update$$anonymous$$assFromShapes: Compute mesh inertia tensor failed for one of the actor's mesh shapes! Please change mesh geometry or supply a tensor manually! UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) $:$$anonymous$$oveNext() (at Assets/Scripts/gun.js:42) UnityEngine.$$anonymous$$onoBehaviour:StartCoroutine_Auto(IEnumerator) gun:Update() (at Assets/Scripts/gun.js:19)
I have searched the error, but I don't have any planes with mesh colliders on them.....
on line 42 of gun.js, it is just where I am instantiating the bullet prefab whenever the player fires the gun... if you want me to show gun.js as well, I can do that.....
-Grady
The script above will be able to detect whether the player is ai$$anonymous$$g at the enemy and whether the player is shooting. The script is checking whether you are ai$$anonymous$$g and shooting at an object with the tag "shootable" which can be assigned to any object. This is done in the inspector where you can create a new tag and name it shootable. Another option would be to change the line
if(hit.collider.gameObject.tag == "shootable")
to
if(hit.collider.gameObject.name == "Enemy").
This is maybe the simpler option for now, but make sure that you set the "Enemy" part to whatever the enemies name is in the hierarchy view. Then you can display in the console whether the enemy was hit by using Debug.Log(); . If you have already got a variable for life of the enemy, then you can subtract from that variable.. Here is an updated version of the script:
var hit : RaycastHit; var EnemyLife : float = 10;
function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, hit, 100)) {
//Here we check if we hit an enemy, be sure to set "Enemy" to
//whatever the enemies name is in the hierarchy view!!
if(hit.collider.gameObject.name == "Enemy")
{
//If player shoots while ai$$anonymous$$g at enemy..
if(Input.GetButtonDown("Fire1"))
{
//subtract from enemy life
EnemyLife -= 1;
//Display message in console
Debug.Log("Bullet hit enemy");
}
}
}
}
That's probably the best way to do it, you could also make the health variable static:
static var health : float = 10;
and then you can access that variable from any script using the script name:
zombie.health--;
with hit.collider.GetComponent(zombie).health--; you are already doing the same thing though, I just thought I'd mention it, glad you got it working ;)