Rigidbody projectile misses collider
I have a projectile in my 2d game that moves at a fairly high speed using it's rigidbody's velocity.
I have a polygon collider attached to my enemy, and if the speed of my projectile is too high, or my player is too close to the enemy, the bullet will pass through the collider without detecting collision.
I think that this is most likely caused by the projectile 'tunnelling' through the collider. By this, I mean that in frame 1 it is just before the collider, and in frame 2 it is passed the collider, so it never actually touches the collider. How can I get around this without reducing the speed of my projectile or increasing the size of my enemy's collider?
Here's the code for the projectile:
Rigidbody2D rigidBody;
public float speed = 75;
public float timeToLive = 3;
public float damage = 1;
void Start () {
rigidBody = GetComponent<Rigidbody2D>();
}
void Update () {
rigidBody.velocity = transform.TransformDirection(Vector3.right * speed);
timeToLive -= Time.deltaTime;
if(timeToLive <= 0){
DestroyImmediate(this.transform.gameObject);
}
}
void OnCollisionEnter2D(Collision2D coll) {
if(coll.gameObject.tag == "Enemy") {
coll.gameObject.GetComponent<EnemyStats>().takeDamage(damage);
}
Destroy(this.transform.gameObject);
}
Answer by toromano · Nov 06, 2015 at 01:11 PM
If you tried selecting rigidbody's collision detection "continuous " too and did not work, my advice would be raycasting the enemy.
Switching to continuous worked! $$anonymous$$any thanks!
If you convert this to an answer I'll gladly accept it.