- Home /
Shooting and health Help
I'm using raycast for my fps and I don't know how to send the message to the enemy when he is hit. here is my shooting script
var bulletSpeed: float = 1000; // bullet speed in meters/second
var shotSound: AudioClip;
function Update(){
if(Input.GetMouseButton){
Fire();
}
}
function Fire(){
if (audio) audio.PlayOneShot(shotSound); // the sound plays immediately
var hit: RaycastHit; // do the exploratory raycast first:
if (Physics.Raycast(transform.position, transform.forward, hit)){
var delay = hit.distance / bulletSpeed; // calculate the flight time
var hitPt = hit.point;
hitPt.y -= delay * 9.8; // calculate the bullet drop at the target
var dir = hitPt - transform.position; // use this to modify the shot direction
yield WaitForSeconds(delay); // wait for the flight time
// then do the actual shooting:
if (Physics.Raycast(transform.position, dir, hit)){
//need it here!!!
}
}
}
So what will I need to add to my script and what will I need to give to my enemy
Since "FPS" is an acronym, it's best to type "FPS" rather than "fps".
The incredibly messy typing and presentation of the 60,000 questions here doesn't help anyone!
look here,
Documentation/ScriptReference/RaycastHit.html
it will show you how to get the collider.
from there, you can easily use
Documentation/ScriptReference/Collider.html
GetComponent() to get to a script attached to the enemy you hit. (Note - you may well also want to check the tag, name, or something to ensure it is an enemy - it could be a space-ship or whatever.)
once you have the script you can call a function, increase damage or whatever is relevent.
in short you use GetComponent()
You don't tell us if it's a network game or not. If yes, you must know how to send informations in the network (like here).