Why does this script only work with a rigidbody? And how can I make it work without a rigidbody? I don’t want to use a rigidbody because my AI reacts poorly when a rigidbody is attached.
var hitPoints = 100.0;
function ApplyDamage (damage : float)
{
if (hitPoints <= 0.0)
return;
hitPoints -= damage;
if (hitPoints <= 0.0)
{
BroadcastMessage ("Detonate");
}
}
function Detonate ()
{
Destroy(gameObject);
}
Rocket Launcher Script
var projectile : Rigidbody;
var initialSpeed = 20.0;
var reloadTime = 0.5;
var ammoCount = 20;
private var lastShot = -10.0;
var damage = 5.0;
function Fire ()
{
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
// Did the time exceed the reload time?
if (Time.time > reloadTime + lastShot && ammoCount > 0)
{
// create a new projectile, use the same position and rotation as the Launcher.
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
// Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
ammoCount--;
}
}
Rocket Script
// 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 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);
}