c# - Unity multiple enemies, raycast dedection? -
i got script create raycast maincamera , hit enemy.
void fixedupdate(){ if (fire) { fire = false; raycasthit hit; if (physics.raycast (fpscam.transform.position, fpscam.transform.forward, out hit, range)){ if (enemy.distance < 80) { if (hit.collider.tag == "body") { debug.log ("bullet in body."); enemy.bodyshot = true; //changing other script variable. } else if (hit.collider.tag == "head") { debug.log ("bullet in head."); enemy.headshot = true; //changing other script variable. } } } } } enemy script in update;
if (headshot) { //headshot variable static reach other script. anim.settrigger ("isdying"); speed = 0; death = true; } if (bodyshot) { //bodyshot variable static reach other script. anim.settrigger ("issore"); } so, when shoot enemy, enemies dying @ same time. because these scripts attached enemies. need change bodyshot , headshot variables without using static. can separate them ?
when comes raycast, you need raycast script attached empty gameobject. not have attached each individual object want perform raycast on.
the time have attach script gameobject want detect clicks on when using unity eventsystem detect clicks on objects.
so, remove script other gameobjects , attach 1 gameobject.
note:
if want access or gameobject ray hit, gameobject stored in hit variable.
raycasthit hit;... destroy(hit.collider.gameobject); you can access scripts attached object hit:
hit.collider.gameobject.getcomponent<yourcomponent>().dosomething();
Comments
Post a Comment