- Home /
Colliding fast moving object with a slow moving object
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.
Answer by Clunk · Sep 28, 2011 at 12:28 AM
All scripting aside for now, try this. Make the rocket a rigidbody. Set collision Detection on the rigidbody settings of the rocket to Continuous Dynamic. Give the enemy a rigidbody collider as well. If necessary, set the enemy's rigidbody to isKinematic, and disable isKinematic upon collision. Make sure the enemy's rigidbody setting for Collision detection is set to "Continuous". If this is not set correctly, for example, it is set to Discrete, the fast moving object will go through other objects. So just keep in mind, "Continuous Dynamic" for fast moving object, "Continuous" for objects the fast mover will collide with.
Your answer
Follow this Question
Related Questions
Ragdoll with multiple colliders 0 Answers
stop objects moving through each other 5 Answers
Adding components to 3d models 1 Answer