- Home /
can anyone write me a enemy damage script?
ive been trying to get my to die when i shoot it but im hopeless at scripting so could anyone write me a script that when i shoot my enemy a few times it will disapear?
This requires more than one script. First, the player script that either instantiates your bullet object or raycasts your bullet hit location. Then you'll need to message the script on your enemy/target to transmit damage and have the enemy script calculate the damage acquired and start the death animation (or destroy the enemy object) if the damage exceeds your preset limit. That isn't coding "a script". Try breaking the necessary actions into steps and coding each step. You'll learn more that way...
It is a waste of time writing a script without knowing your game classes and other details. This question looks like a joke.
there is never a better time than the present to learn how to code!
Answer by Statement · Dec 07, 2010 at 04:44 PM
In general you can do this:
- Wait for a mouse click.
- Create a ray that travels from your camera in the direction it's facing.
- Check the first thing it hits, is it your enemy? If so;
- Subtract a health point from enemy.
- If enemy health reaches zero, destroy it.
Shooter.cs - put this on your camera.
using UnityEngine;
public class Shooter : MonoBehaviour { void Update ( ) { // 1. Wait for a mouse click. if ( Input.GetButtonDown( "Fire1" ) ) { Shoot( ); } }
void Shoot ( )
{
// 2. Create a ray that travels from your camera
// in the direction it's facing.
Ray ray = new Ray( transform.position, transform.forward );
RaycastHit hit;
if ( Physics.Raycast( ray, out hit ) )
{
// 3. Check the first thing it hits, is it your enemy?
// If so (actually there is nothing defined as enemy);
hit.transform.SendMessage( "OnBullet",
SendMessageOptions.DontRequireReceiver );
}
}
}
Shootable.cs - put this on your enemy with a collider attached to it.
using UnityEngine;
public class Shootable : MonoBehaviour { public int health = 5;
void OnBullet ( )
{
Damage( );
}
void Damage ( )
{
// 3.1. Subtract a health point from enemy.
health = health - 1;
// 3.2. If enemy health reaches zero, destroy it.
if ( health == 0 )
{
Kill( );
}
}
void Kill ( )
{
Destroy( gameObject );
}
}
Hopefully this skeleton will get you adding effects on your own. Pick up coding. People won't endlessly hand you scripts. Though I know the frustration if you're an artist for example and need code, or the other way around.
Answer by omegamontage · Mar 12, 2012 at 12:42 AM
var hitPoints = 100.0; var deadReplacement : Transform; var dieSound : AudioClip;
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)
{
Detonate();
}
}
function Detonate () { // Destroy ourselves Destroy(gameObject);
// Play a dying audio clip
if (dieSound)
AudioSource.PlayClipAtPoint(dieSound, transform.position);
// Replace ourselves with the dead body
if (deadReplacement) {
var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
// Copy position & rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse(transform, dead);
}
}
static function CopyTransformsRecurse (src : Transform, dst : Transform) { dst.position = src.position; dst.rotation = src.rotation;
for (var child : Transform in dst) {
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
Answer by Eddieg26 · Feb 23, 2013 at 01:58 AM
var health : float = 100.0;
var maxHealth : float = 100.0;
var healthBar : float =Screen.width/2
function Update(){
AddjustHealth(0);
if(health < 0)
health = 0;
if(maxHealth < 0)
maxHealth = 0;
}
function OnGUI(){
GUI.Box(Rect(10,10,healthBar *(health / maxHealth),health);
}
function AddjustHealth(adj : int){
health += adj;
}
Your answer
Follow this Question
Related Questions
Detect enemy on animation 1 Answer
Temporary invulnerability in a platformer 1 Answer
Collision Damage 1 Answer
(C#) Enemy health and take damage from bullets 1 Answer
How to have an NPC with multiple parts detect damage 1 Answer