Hi peops. Firstly, I’m not quite sure which area to post this in, but as it only seems to happen on an orthographic camera I thought I’d add it in 2D.
Anyway, the issue. I have two virtually identical scripts on two objects which are sprites as follows…
public class PlayerCollisionChecker : MonoBehaviour {
public Transform explosionEffect;
private Transform player;
void Start() {
player = transform;
}
void OnCollisionEnter2D(Collision2D collision) {
Debug.Log("Player collision with " + collision.gameObject.name);
switch(collision.gameObject.name) {
case "enemy0":
Instantiate(explosionEffect, player.position, Quaternion.identity);
player.GetComponent<PlayerControl>().resetPlayerPosition();
break;
}
}
}
and
public class EnemyCollisionChecker : MonoBehaviour {
public Transform explosionEffect;
private Transform enemy;
void Start() {
enemy = transform;
}
void OnCollisionEnter2D(Collision2D collision) {
Debug.Log("Enemy collision with " + collision.gameObject.name);
switch(collision.gameObject.name) {
case "Player Spaceship":
Instantiate(explosionEffect, enemy.position, Quaternion.identity);
enemy.GetComponent<EnemyAI>().resetEnemy();
break;
}
}
}
However, only one of the objects is detecting a collision, and it’s not always the same one but I never get both collision detection at the same time.
Both my sprites have a Rigidbody2D and BoxCollider2D set on them and the fact that one or the other detects collision but not both at the same time obviously means that it’s working.
Should I be adding a delay before resetting the player/enemy? All that the reset function does is to simply relocate the sprite back to its original position (and hence, removes the collision but the original one should have already been detected).
Any help greatly appreciated as this is currently confusing me big time.
Chris