- Home /
Help with dammage script
Hi I am quite new to Unity and i have been following a few video tutorials to help me learn. I have created a basic level and have two sentry guns which work and fire and cause damage to my first person controller (all good so far)
The problem I have is causing damage and destroying my sentry guns, I have use the same type of scripts like the player health, but something strange happens, both the sentry guns explode as soon as I shoot regardless of the damage or if the bullets hit.
Can anyone post a simple script one for my projectile to cause damage and one for the sentry gun to receive it I would be grateful.
well, you should really post the part of your code that handles the damage, so we can see if it just needs a little work, but I cant help you if I dont know what your working with now.
Answer by CaioRosisca · Sep 26, 2012 at 08:15 PM
You can try this:
to apply damage to an object you can do the following:
when your bullet collides with the object that will receive the damage do this:
apply this script to your bullet.
void OnCollisionEnter(Collision col)
{
col.gameObject.SendMessage ("ApplyDamage", 5.0, SendMessageOptions.DontRequireReceiver);
}
gameObject must be the gameObject that will receive the damage(you can assign it or use the parameter received with your colliding function)..
then on the damage receiver object create this function:
void ApplyDamage (Float damage) {
currentHeal -= damage;
print (damage);
}
this will remove X hitPoints from your current health.
Answer by Madmogga · Sep 26, 2012 at 09:12 PM
Sorry was at work at the time, my dammage script is as follows
var rayCastLength = 5;
function Update () { var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, rayCastLength)) { if(hit.collider.gameObject.tag == "Mini");
Healthminigun.GUNHEALTH -=50; Destroy (gameObject,.2); }
}
Answer by Madmogga · Sep 26, 2012 at 09:12 PM
sorry i guess it would help if i posted my script, below is what i have on my projectile
var rayCastLength = 5;
function Update () { var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, rayCastLength)) { if(hit.collider.gameObject.tag == "Mini");
Healthminigun.GUNHEALTH -=50; Destroy (gameObject,.2); }
}
and this is what is on my sentri gun
var explosion : GameObject; static var GUNHEALTH = 100;
function Update () {
if(GUNHEALTH <=0) { var explosionClone = Instantiate(explosion,transform.position, transform.rotation); explosionClone.GetComponent("Detonator"); Destroy (gameObject,.6); Destroy (rigidbody); }
}
i am copying snipts of code trying to understand what each does so any help please
thanks
You should edit your original question with the additional information ins$$anonymous$$d of adding new answers that are not really answers. I know it can be tempting to use this website like a forum, but it really isn't meant to be used that way.
O$$anonymous$$.. when you calculate your Raycast inside Update() this means it will be checking for that hit everyframe, so if you deal 50 damage to your turret in case the raycast it hitting it, within 2 frames you will have dealt 100 damage(turret´s total health). That´s why it gets destroyed so fast, so ins$$anonymous$$d of using a raycast to calculate if you have hit your target try to use a collision detection like I´ve said on my answer. For this you just need a collider on your projectile. If you do this and use the code I have posted on my answer your turret will detect it has been hit just once.
To make sure that you are apllying damage too often, after this line of your code:
Health$$anonymous$$igun.GUNHEALTH -=50; Destroy (gameObject,.2);
put this:
print("Damage applied");
and watch your console window to see how amny times if happens.
also, if using my solution after:
"col.gameObject.Send$$anonymous$$essage ("ApplyDamage", 5.0, Send$$anonymous$$essageOptions.DontRequireReceiver);"
destroy your projectile using:
Destroy (gameObject)
Your answer
Follow this Question
Related Questions
How to make Enemy AI and Health 1 Answer
Enemy Health Damage 2 Answers
How can I lose health when my enemy collides with the player? 1 Answer
House/building losing health 3 Answers
Shooting and health Help 0 Answers