How to do damage

Hi. I'm somewhat new to Unity and I have been following the FPS tutorial and tried to apply what i learned to my project but I can't get my bullets to do damage. I've used the scripts they did but i'm not sure why none of it is working. At the moment I'm just trying to get my camera to shoot at a block and everything works but the damage. Someone please help me understand the script!

3 Answers

3

You can do this in all kinds of ways, I just make an rpc call when a bullet collides with something that can take damage (tagged). I pass around a string of the name of player that fired, and in the receiving rpc call, I pass in the name of the shooter and name of who was hit, find who was hit in the local scene graph and do the damage calc

I've been looking into what exactly an rpc is and how to use it and i still dont totaly understand. I'm sorry but I'm really new to all this and I'm teaching myself Java as I go. Where could I learn more about rpc's?

I'm trying this script and ataching it to the cube i want to destroy and hitting that cube with bullets and the bullets subtract 10 health every hit. I keep getting the message that after the function before update it expects a ( and I dont understand why. //opponent has 100 health var health=100; //Have the object take damage if a bullet hits it function OnCollisionEnter(collision : other){ if(other.gameobject.tag=="Bullet") { health-=10;} function Update(){ //If health is zero object disapears if(health<=0); Destroy(gameobject); } } Thanks for the help

So I found the answer through this tutorial: http://www.youtube.com/watch?v=WmXZDSEK65U

I used triggers and the tags like you said but the tutorial really helped make it clear as to what exactly is going on and how to use them. I made two seperate scripts one fro the enemy:

var health=10;

function OnDamage(damage:int)

{

//subtract health by one

health --;

//if health is less than or equal to 0

if(health <- 0)

{

//destroy object

Destroy(gameObject);

}

}

and one for the bullet itself:

//Bullet

//how fast does the shot move

var speed:float;

var damage:int=-1;

function Update()

{

//each frame move forward by speed units per second

transform.position.z += speed *Time.deltaTime;

}

//gets called when we hit something

function OnTriggerEnter(other:Collider)

{

//what are we hitting? we are only interested in hitting "enemy"

if(other.gameObject.CompareTag("Enemy"))

{
//send a messege telling the object to take damage

other.gameObject.SendMessage("OnDamage", damage);

//destroy object

Destroy(gameObject);

}

}

I added a tag to the bullet and to the enemy. I hope this helps someone who had problems with this like I did!

I don't understand where or why you added a tag to the bullet and I tried your code it didn't work for me. Bullets just bounce off whatever they hit.

sorry i forgot to mention you have to make the box a trigger. Now for some reason the box then just falls and I haven't figured out why yet. I don't think I've actually used the bullet tag yet either, at least not in this script. But I added it just in case I need it later.