- Home /
Kill one enemy they all dies?
Hi
The problem with my code is that when i fire at a enemy, they all take damage. They all using the same "enemy" tag, and according to other treads i guess i somehow need to turn each enemy into something other than static. Im not really sure how to do this. I tried to put "public#34; and/or "private&" in front of each related variabel ("var"), but that did not made any diffrence.
This is the code fore the bullet:
var maxDist : float = 1000000;
var decalHitWall : GameObject;
var floatInFrontOfWall : float = 0.0001;
var damage : float = 1;
var GetFSM : PlayMakerFSM;
GetFSM = gameObject.Find("Maltavla").GetComponent.<PlayMakerFSM>();
function Update ()
{
var hit : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit, maxDist))
{
if (decalHitWall && hit.transform.tag == "LevelParts")
Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
if (hit.transform.tag == "Enemy")
{
hit.transform.GetComponent(EnemyBodyScript).enemyHealth -= damage;
// GetComponent(EnemyBodyPartsScript).enemyBody.
}
if (hit.transform.tag == "EnemyHead")
{// activate playmaker event "headshot"
GetFSM.Fsm.Event("HeadShot");
}
}
Destroy(gameObject);
}
And this is the code i use for the enemy:
var enemyHealth : float = 10;
var enemyHeadHealth : float = 1;
var GetFSM : PlayMakerFSM;
GetFSM = gameObject.Find("Maltavla").GetComponent.<PlayMakerFSM>();
function Update ()
{
if (enemyHealth <= 0)
// activate playmaker event
GetFSM.Fsm.Event("Killed");
As you can see im also using playmaker.
Regards,
"public#34; and/or "private&" is suppose to be "public" and/or "private"
i think you skipped some code... or did you in Update making Physics.Raycasts and destroying object without any checking for conditions? i can't found any critical error here.
This is not the answer, just some comments
Do you really want to do a raycast each frame? Why don't you use a collider?
gameObject.Find() is incorrect: GameObject.Find() is.
yea, you're not showing the relevant part of the code.. We need to see the part where the enemy is actually destroyed..
as Scroodge$$anonymous$$ says, looks like the enemyHealth part should work right..
Answer by ScroodgeM · Aug 29, 2012 at 09:09 PM
found some error in your second code piece:
GetFSM = gameObject.Find("Maltavla").GetComponent.<PlayMakerFSM>();
this founds ANY gameobject named "Maltavla"
if (enemyHealth <= 0) GetFSM.Fsm.Event("Killed");
and this kills it checking LOCAL variable enemyHealth
but how do you map enemyHealth to GetFSM? is this enemyHealth for THIS GetFSM&
Yep, that's the problem. It finds the "first" gameobject it finds that is named "$$anonymous$$altavla". So all enemies "find" the same object. If you want to access the FS$$anonymous$$ component on the current enemy, just use GetComponent:
var GetFS$$anonymous$$ : Play$$anonymous$$akerFS$$anonymous$$;
GetFS$$anonymous$$ = GetComponent(Play$$anonymous$$akerFS$$anonymous$$);
In your bullet script you want to get the FS$$anonymous$$ component of the object you hit, so you have to do the same as for your EnemyBodyScript
// [...]
var FS$$anonymous$$ = hit.transform.GetComponent(Play$$anonymous$$akerFS$$anonymous$$);
S$$anonymous$$.Fsm.Event("HeadShot");
You might get a problem because you can hit different objects on one enemy, so i guess the "head" object doesn't have the FS$$anonymous$$ attached, One way is to use this ins$$anonymous$$d:
var FS$$anonymous$$ = hit.transform.root.GetComponentInChildren(Play$$anonymous$$akerFS$$anonymous$$);
This will take the object you've hit, go up the hierarchy to the very top and then search for a Play$$anonymous$$akerFS$$anonymous$$ component on any childs of this object.
Answer by sobek · Aug 29, 2012 at 09:35 PM
Hi Thank you for the replay, but i dont really understand your question. This is the enemy life, its 10:
var enemyHealth : float = 10;
and this is the damage that is being made once the bullets raycast reach the enemy:
var damage : float = 1;
" GetFSM = gameObject.Find("Maltavla").GetComponent.();
this founds ANY gameobject named "Maltavla" "
Yes it does, what i need is a code to find only the one gameobject that was hit
is this script attached to enemy? that simple replace
GetFS$$anonymous$$ = gameObject.Find("$$anonymous$$altavla").GetComponent.<Play$$anonymous$$akerFS$$anonymous$$>();
with
GetFS$$anonymous$$ = GetComponent.<Play$$anonymous$$akerFS$$anonymous$$>();
Answer by sobek · Aug 29, 2012 at 10:44 PM
ScroodgeM: Your idea seems to work! Thank you :D
Please familiarize yourself with the comment function on this site. Comments to existing answers should not be answers themselves. (You can convert your answers to comments and vice versa)