Everything was working fine, and then all of a sudden the collision gave out. I’ve done quite a bit of testing with this one and can’t figure it out. The basic set up is the player can fire a bullet, which is instantiated. the bullet has the following script on it (most of this code is not related to this problem and works just fine, I’m mostly concerned with the OnCollisionEnter() part):
` using UnityEngine; using System.Collections; public class Bullet : MonoBehaviour { float bulletSpeed = 5.0f; // speed of the bullet to travel GameObject cam; // used to ref camera object in order to destroy bullet if it has left the camera view Movement mover; public string player; BoxCollider colBox; // Vector3 bulletPosition; float bulletDirectionX; float bulletDirectionY; public int playerNum; // Use this for initialization void Start () { bulletPosition = new Vector3(0, 0, 0); if (playerNum == 1)//assigned in PlayerFire.cs mover = GameObject.Find("Player1").GetComponent(typeof(Movement)) as Movement; if (playerNum == 2)//assigned in PlayerFire.cs mover = GameObject.Find("Player2").GetComponent(typeof(Movement)) as Movement; cam = GameObject.Find("Main Camera"); colBox = GetComponent(typeof(BoxCollider)) as BoxCollider; bulletDirectionX = mover.fireDirectionX; bulletDirectionY = mover.fireDirectionY; } // Update is called once per frame void Update () { //move bullet //check for collision //check for leaving view bulletPosition.x = bulletDirectionX*Time.deltaTime*bulletSpeed; bulletPosition.y = bulletDirectionY*Time.deltaTime*bulletSpeed; colBox.transform.Translate(bulletPosition); } void OnBecameInvisible () { Destroy(gameObject); } void OnCollisionEnter(Collision objHit) { if(objHit.gameObject.tag == "enemy" && GlobalSettings.gameState != GlobalSettings.GameState.cutScene){ Destroy(objHit.gameObject); Destroy(gameObject); } } }`
The enemy has the following code in it:
` using UnityEngine; using System.Collections; //controls the enemy basic movement //includes: //moving to player //jumping over pits //firing at player public class Enemy : MonoBehaviour { float moveSpeed; GameObject player1; Vector3 attDirection; void Start () { player1 = GameObject.Find("Player1"); moveSpeed = 3.0f; } void FixedUpdate () { attDirection = player1.transform.position; moveToPlayer(); } void moveToPlayer() { transform.position = Vector3.MoveTowards(transform.position, new Vector3(attDirection.x, transform.position.y, transform.position.z), Time.deltaTime*moveSpeed); } }`
If the enemy is walking around, it will be destroyed just fine. But, if the enemy is in contact with the player, or with a special edge detection object, it cannot be destroyed and the bullet simply ignores the collision. What’s more, is that if I wiggle the player around when the enemy is on top of it, I can get it back to a state in which the bullet can destroy it again. Doing this does not throw a OnCollisionExit() function if added to it.
I reduced this down to using a basic cube, cube collider, and a rigid body on a block as the enemy. The bullet is a box collider. I suspect that something is happening to the enemy once it triggers a specific type of collision that makes it no longer trigger collisions.