Hello!
I have been trying for a while to create a projectile (GameObject with box collider and RigidBody like the Rocket in the FPS Tutorials) that will only collide with a CharacterController.
I would like to create a ball that bounces off of walls and other basic colliders and explodes when it touches a CharacterController.
Here is my code that I have right now:
// The reference to the explosion prefab
var explosion : GameObject;
var timeOut = 3.0;
// Kill the rocket after a while automatically
function Start () {
Invoke("Kill", timeOut);
}
//function OnControllerColliderHit (hit : ControllerColliderHit )
//{
// // Instantiate explosion at the impact point and rotate the explosion
// // so that the y-axis faces along the surface normal
// var rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
// Instantiate (explosion, hit.point, rotation);
//
// // And kill our selves
// Kill ();
//}
function OnCollisionEnter (collision : Collision) {
// Instantiate explosion at the impact point and rotate the explosion
// so that the y-axis faces along the surface normal
var contact : ContactPoint = collision.contacts[0];
var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
Instantiate (explosion, contact.point, rotation);
// And kill our selves
Kill ();
}
function Kill () {
// Stop emitting particles in any children
var emitter : ParticleEmitter= GetComponentInChildren(ParticleEmitter);
if (emitter)
emitter.emit = false;
// Detach children - We do this to detach the trail rendererer which should be set up to auto destruct
transform.DetachChildren();
// Destroy the projectile
Destroy(gameObject);
}
@script RequireComponent (Rigidbody)
I tried using OnControllerColliderHit, but the projectiles still passed through my character.
Thanks in advance for your help![/code]