- Home /
Issues NPC Grenade avoidance
Hello this is my first time here, I have an issues about NPC especially for Grenade Avoidance. Let take example: NPC see grenade near them --> They avoid it by going out of range--> The Script work! but when about to shout because awareness of "Grenade!!". It's not working as expected. Here example:
Function GrenadeRun(){
while (true) {
if ( GrenadeDistance < AwareGrenadeRange ) {
//Shout grenade "GRENADE!" script
}
if ( GrenadeDistance < RunGrenadeRange ) {
//Run from grenade
}
yield;
}
}
The script result: NPC run away from grenade but shout the "grenade!" frame-by-frame. Undesired result. So i try this way:
Function GrenadeRun(){
while (true) {
if ( GrenadeDistance < AwareGrenadeRange ) {
//Shout grenade "GRENADE!" script
yield WaitForSeconds(1);
}
if ( GrenadeDistance < RunGrenadeRange ) {
//Run from grenade
}
yield;
}
}
The script result: The NPC shout "grenade!" is ok now but need to wait 1 second before start run away from grenade. Undesired result. Another attempt:
Function GrenadeRun(){
while (true) {
if ( GrenadeDistance < AwareGrenadeRange ) {
//Shout grenade "GRENADE!" script
yield WaitForSeconds(1);
}
}
while (true) {
if ( GrenadeDistance < RunGrenadeRange ) {
//Run from grenade
}
yield;
}
}
The script result: The NPC only shout "GRENADE!" only. Not running from grenade What i want is the NPC shout "Grenade!" per second and start to run simultaneously. but i still cant figure it out. Thank you
Answer by Azran · Sep 11, 2012 at 02:42 PM
The problem here is that you are calling shout inside the loop.
You can use a variable which hold the last time you shouted. Here is my pseudocode
if (lastTimeShouted < Time.Now - 1sec)
{
shoutAgain();
lastTimeShouted = Time.Now
}
Answer by XenoAisam · Sep 11, 2012 at 01:16 PM
i have tried it.. its still not working. It's still produce "Grenade!" frame-by-frame. To be honest, i still want to retain the use of "while(true}" in the code: here my code:
function GrenadeRun () {
var curWayPoint = AutoWayPoint.FindClosest(transform.position);
var lastVisibleGrenadePosition = GrenadeTar.transform.position;
if (CanSeeGrenade ()){
while (true) {
var waypointPosition = curWayPoint.transform.position;
if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance)
curWayPoint = PickNextWaypoint (curWayPoint);
var distance = Vector3.Distance(transform.position, GrenadeTar.transform.position);
if ( distance < AwareGrenadeRange ) {
GrenadaWarning();
}
if ( distance < AwareGrenadeRange) {
MoveTowardsRun(waypointPosition);
} else {
BackToPatrol ();
return;
}
if (CanSeeTarget () && !CanSeeGrenade()) {
yield StartCoroutine("AttackPlayer");
return;
}
yield;
}
}
}
And the targeted function of GrenadaWarning();
function GrenadaWarning () {
var distance = Vector3.Distance(transform.position, GrenadeTar.transform.position);
while (distance < AwareGrenadeRange) {
BroadcastMessage("GrenWarnStarter");
yield WaitForSeconds(1.5);
}
}
Still make the NPC shout continuously frame-by-frame
Answer by XenoAisam · Sep 11, 2012 at 03:19 PM
Azran answer solved my issues :D Thank you Azran. Next time i will try to learn more about unity script especially Invoke timing too.
here the working script:
declare at header (JavaScript):
private var lastTimeShouted = 0.0;
The Shout one:
function GrenadaWarning () {
if (lastTimeShouted < Time.time - 1)
BroadcastMessage("GrenWarnStarter");
lastTimeShouted = Time.time;
}
i feel very shame.. its just a few line too but work as my expecting
You really need to look at the incredibly simple "Invoke" command. there is no reason to use Yield, etc for such simple situations.