I read few tutorials and examined several examples of collision detection, but was not able to understand how to do it properly. Each tutorial/example seems to use different technique.
I did some small demo with fast rockets and slow enemies, but it does not work as expected. The rockets sometimes "explode" in thin air, and when colliding with an enemy, only the rocket explodes (the enemy should explode as well).
I hope someone could sort it out and point me to the right technique to use.
I use the following method to detect a collision for an enemy:
var speed;
var explosion : GameObject;
function FixedUpdate ()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
function OnCollisionEnter(collision : Collision) {
if (collision.gameObject.tag != "Plane") {
var contact : ContactPoint = collision.contacts[0];
if(explosion) Instantiate(explosion, contact.point, Quaternion.LookRotation(contact.normal) );
Destroy(gameObject);
}
}
Enemy is a Box collider and rigid body with "continuous collision detection"
In rocket, I use the following collision script:
function Update ()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, speed * Time.deltaTime))
{
if(explosion) Instantiate(explosion, hit.point, Quaternion.LookRotation(hit.normal) );
Destroy(gameObject);
}
}
The rocket is not rigidbody.