Im working on a shooting script for a FPS game. I have two parts, “shoot” attached to the game camera and “Take damage” attached to the target. Im not quite sure what the raycast hit should be checking for though. Everything’s written in Java script.
Shoot (attached to the player)
var Wcenter:int;
Wcenter=(Mathf.Round(Camera.main.pixelHeight/2));
var Hcenter:int;
Hcenter=(Mathf.Round(Camera.main.pixelWidth/2));
static var ray:Ray;
static var hit : RaycastHit;
function Update ()
{
var ray = Camera.main.ScreenPointToRay (Vector2(Hcenter,Wcenter));
if(Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (ray, hit, 100))
{
Debug.DrawLine (ray.origin, hit.point);
}
}
}
Take damage (attached to the monster)
var ray=(shoot.ray);
var hit=(shoot.hit);
[COLOR="red"]var Monster: Transform[/COLOR];
function Update ()
{
if (Input.GetMouseButtonDown(0))
{
if (hit == Monster)
{
print ("hit!");
}
}
}
Essentially im looking for a way to set the Monster variable to the parent object and still be able to check if it was hit by a ray cast. Thanks for your time!
Edit: Eck, I probably could have had a more descriptive title. Sorry!
That’s not exactly what im looking for, I want to be able to attach the script to my enemy like i do now, that way i can create/destroy them without having to recycle a lot of variables. The more information i keep on the enemy the easier it is. Im looking for something I can replace the monster variable with that will trigger the ray cast hit when it does hit the monster. Thanks for the reply!
thats the whole point of the raycast if i understand what you want correctly.
you put your damage script on the enemy and shoot a ray.
you check on the player if it hits the enemy and when it does, you can send a message to that enemy to destroy it.
Kinda, but i think it would be easier to check on the enemy if the ray cast hit it, rather than figuring out what enemy was hit. I need something like the transform, but will trigger the raycast actually hitting it. Right now the raycast doesn’t realize its touched the enemy.
yes it does, thats what the ‘hit’ variable is for, the object you hit with the raycast.
i don’t see the point in using a raycast if your not using it to determine the object that got hit by it.
Im using the raycast to predict the path the bullet would take as well as what object was hit. if i do that check in the player, then set a health variable on the enemy it gets complicated. If i make the raycast global and then check on the enemy to see if its intersecting that raycast i can set the local health variable for that enemy.
If i do that check from the player, then i have to figure out where in the array that enemy is and then set its health. If i do the check from the unit i can set a local variable and keep everything clean and simple.
Edit: Sorry if ive caused any confusion here appels. I really appreciate your support here.
Nope, you still don’t get it. Hit is the object, no need to find anything in any array.
hit.collider.gameobject is the object. thats it.
Anyway… read the documentation…
Good luck
Well the easiest way to do what you want is to simplify it a bit more…
in your shoot script have :
var Wcenter:int;
Wcenter=(Mathf.Round(Camera.main.pixelHeight/2));
var Hcenter:int;
Hcenter=(Mathf.Round(Camera.main.pixelWidth/2));
static var ray:Ray;
static var hit : RaycastHit;
//========== Add this in here =================
var damage : float; // the damage you want you hit object to take.... if you don't like float...go int
//==============
function Update ()
{
var ray = Camera.main.ScreenPointToRay (Vector2(Hcenter,Wcenter));
if(Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (ray, hit, 100))
{
Debug.DrawLine (ray.origin, hit.point);
// Send a message to any hit object... tell it that it was hit and the damage recieved
hit.transform.SendMessage("Tag_Youre_Hit", damage, SendMessageOptions.DontRequireReciever);
}
}
}
then just have a function in youre enemy script like… well its more like scrap youre enemy code and just have something like this:
var health: float;
function Tag_Youre_Hit(d : float)
{
print("I was hit for " + d + " damage");
health -= d; // take damage from health
}
sorry if some code doesn’t work exactly as typed… I haven’t used unityscript in quite some time so some c# style syntax may have poked through my attempt at text coding unityscript. But this method lets you have a health / enemy script on each enemy… each enemy keeps track of its own health and your bullets will effect the health of whatever enemy you hit.
Thanks for the help Appels and Nova! That did the trick, now just to disect a few of statements.
Thanks again! I spent the last few days trying to get the weapon system working with the wrong approach it looks like.