- Home /
DamageScript
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);
}
Is it my Rocket Launcher Script from the Fps tut then? Or my Rocket script?
I have a cube with ai and it is supposed to follow the player. When a rigid body is attached to the cube it can be killed but just hovers in the air, when no rigid body is attached the cube chases the player but cannot be killed. I want my cube to chase the player and be able to be killed.
Answer by adrenak · Jan 13, 2012 at 07:26 PM
In the FPS tutorial the explosion prefab that is instantiated when the rocked hits any collider, its script searches for rigidbodys near it so the forces can be applied to them, if there is any rigidbody near it, then it applies force and then it calls for ApplyDamage function in them. The reason why your enemy cube is not destroyed when you fire the rocket is because its not a rigidbody.
To solve the problem do this :
tag all the enemies as "enemy" using the tag manager
in the script attached to the explosion prefab, there are these statements
for (var hit in colliders) { if (!hit) continue; if (hit.rigidbody) { hit.rigidbody.AddExplosionForce(explosionPower, explosionPosition, explosionRadius, 3.0); var closestPoint = hit.rigidbody.ClosestPointOnBounds(explosionPosition); var distance = Vector3.Distance(closestPoint, explosionPosition); // The hit points we apply fall decrease with distance from the hit point var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius); hitPoints *= explosionDamage; // Tell the rigidbody or any other script attached to the hit object // how much damage is to be applied! hit.rigidbody.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver); }
Here you can see that there are 2 if statements , they are : if(!hit) AND if(hit.rigidbody). Add these statements inside the for loop along with them.
if(hit.gameObject.tag == "enemy"){
hit.gameObject.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
}
This makes sure that ApplyDamage function is called in enemies that are without rigidbody components.
Hope this helps! Tell me if you have any problems.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Damage taking? 1 Answer
Attach object as child using code 1 Answer
Help with Basic Movement Script 1 Answer
NEED HELP CODING, CANT FIND MISTAKE 2 Answers