- Home /
Ai Aint Taking Damage From Bullet (FPS Rocket)
hello guys my english isn't that good but im doing my best to explain my problem as best as i can so for start i have 2 scripts the damage reciever and the rocket script both from the fps tutorial well i edited the rocket script a little bit so i would get rid off the explosion area damage and just do damage on hit/collide so i can use it for my ak-47 but it doesn't does any damage and i dont get errors or anything and im stuck on this for about 3 days before those 3 days every bullet with the ak-47 was a one shot kill even if i put the damage to 0 well il show the scripts
THE ROCKET SCRIPT
var explosion : GameObject;
var timeOut = 3.0;
var damage = 10;
function Start () {
Invoke("Kill", timeOut);
}
function OnCollison (hit :Collision) {
// Tell the object you hit that it was damaged
hit.CharacterController.SendMessageUpwards("ApplyDamage", damage,SendMessageOptions.DontRequireReceiver);
// 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)
------------------------------------------------------------
THE DAMAGERECIEVER SCRIPT
var hitPoints = 100.0;
var detonationDelay = 0.0;
var explosion : Transform;
var deadReplacement : Rigidbody;
function ApplyDamage (damage : float) {
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
return;
hitPoints -= damage;
if (hitPoints <= 0.0) {
// Start emitting particles
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter)
emitter.emit = true;
Invoke("DelayedDetonate", detonationDelay);
}
}
function DelayedDetonate () {
BroadcastMessage ("Detonate");
}
function Detonate () {
// Destroy ourselves
Destroy(gameObject);
// Create the explosion
if (explosion)
Instantiate (explosion, transform.position, transform.rotation);
// If we have a dead barrel then replace ourselves with it!
if (deadReplacement) {
var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);
// For better effect we assign the same velocity to the exploded barrel
dead.rigidbody.velocity = rigidbody.velocity;
dead.angularVelocity = rigidbody.angularVelocity;
}
// If there is a particle emitter stop emitting and detach so it doesnt get destroyed
// right away
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter) {
emitter.emit = false;
emitter.transform.parent = null;
}
}
// We require the barrel to be a rigidbody, so that it can do nice physics
@script RequireComponent (Rigidbody)
i think the problem is in the rocket script i think the function call of ApplyDamage isn't going good and the weird thing is the real rocket script with area damage and everything works fine i just need the bullet to on hit call function applydamage in the damagereciever and the best thing would be if i can edit the damage in the bullet (rocket script) by a var i tried a bunch of different codes and i just didnt managet to get it fixed
so after 3 days im gonna ask the pro's hope u guys can help me
guys guys can no one help me ? or is it to difficult but a little help would be great
I did not read the hole thing but you have a function called " OnCollison" there is no built in function named that. You should properly change it to "OnCollisionEnter(hit : Collision)
already tried didnt work i thought with oncollisionenter it's about the bullet that needs to enter the collider so oncollision just on touch but both didnt work
i already fixed it with total other script this one was driving me insane but i liked it more because there were real bullets
I didnt read it but if its what i think you were trying to do then you cant use colliders for bullets that are at anywhere near bullet speed because in the smallest unity of testing (ie a frame) the bullet could have essentially teleported past the object you were ai$$anonymous$$g at.
best to use bullets for visual and raycasting for the mechanics.
Answer by aldonaletto · Nov 17, 2011 at 12:36 AM
This line is wrong:
hit.CharacterController.SendMessageUpwards("ApplyDamage", damage,SendMessageOptions.DontRequireReceiver);
hit is a Collision, and it doesn't have a CharacterController field. You could try this:
hit.transform.SendMessageUpwards("ApplyDamage", damage,SendMessageOptions.DontRequireReceiver);
You should also use CharacterDamage.js to receive damage in CharacterControllers - DamageReceiver.js is intended to be used with rigidbodies, and will produce runtime errors. Another thing: weapons with fast bullets like guns and rifles should use Raycast to shoot (fast projectiles often are not detected) - take a look at the MachineGun.js script.
Your answer
Follow this Question
Related Questions
Ai that applies damage in collision? 1 Answer
Damage trigger? 1 Answer
My rockets don't do damage? 1 Answer
Area of effect melee attack for 3rd person game? 1 Answer
FPS Tutorial AI Damage not Working 1 Answer