i’m a total newbie at the end point of my thesis project, i think this might be the final script…!!! thank you all for great documentation and tutorials, esp the FPS tutorials.
basically im trying to do this:
spawn @ start point
die
respawn @ randompoint 1
die
respawn @randompoint 2 …
i think i need this bit of code to continue cycling, but the “while” statement is crashing Unity.
if (respawnOccurred > 0){
spawnitems();
}
anyone know how to fix this?
also - do you know if this (or my other messy codes) might be expensive on my cpu?
thanks thanks!!
heres the full code:
var spawnRange = 0.0;
var gizmoName : String;
var enemyPrefab : GameObject;
var respawnOccurred : int = 0;
var respawnLimit: int = 1;
var numPointsNeeded = 5;
var numLeftToTry : float = 10.0;
private var player : Transform;
private var currentEnemy : GameObject;
private var wasOutside = true;
function Start ()
{
player = GameObject.FindWithTag("Player").transform;
}
function Update ()
{
Debug.Log ("respawnOccurred");
if (respawnOccurred == 0){
var distanceToPlayer = Vector3.Distance(transform.position, player.position);
if (distanceToPlayer < spawnRange)
{
if (!currentEnemy wasOutside)
currentEnemy = Instantiate(enemyPrefab, transform.position, transform.rotation);
respawnOccurred++;
if (respawnOccurred > 0){
spawnitems();
}
wasOutside = false;
}
else
{
if (currentEnemy !wasOutside){
Destroy(currentEnemy);
}
wasOutside = true;
}
}
}
function spawnitems()
{
var spawns = GameObject.FindGameObjectsWithTag("Respawn");
Debug.Log("randomRespawn");
for (j=0;j<10.0;j++) {
var numLeftToTry = 10.0 - j;
var chance = numPointsNeeded / numLeftToTry;
if (Random.value <= chance)
if ((chance > 0) (Random.value <= chance)){
var thisSpawnPoint : GameObject = spawns[j];
Instantiate(enemyPrefab, thisSpawnPoint.transform.position, thisSpawnPoint.transform.rotation);
numPointsNeeded--;
}
}
}
hi there, yes. it spawns the npc only when Player is in range, and destroys when Player is out of range.
And the npc is also connected to CharacterDamage script so each time Player kills the npc, this script is meant to respawn it in another random spawn point (in my array)…
After a quick glance at your code I found an error in the following part:
if (!currentEnemy wasOutside)
currentEnemy = Instantiate(enemyPrefab, transform.position, transform.rotation);
respawnOccurred++;
if (respawnOccurred > 0){
spawnitems();
}
(respawnOccurred > 0) is always true, because one line above it, you increase the value by one. I think you forgot the brackets in the if statement above. So with brackets it would be:
if (!currentEnemy wasOutside){
currentEnemy = Instantiate(enemyPrefab, transform.position, transform.rotation);
respawnOccurred++;
}
if (respawnOccurred > 0){
spawnitems();
}
The code only runs once because “respawnOccurred” is never set to 0. In the beginning of the Update() method (which runs every frame) you check if respawnOccurred is 0, and this is only true one time.
i really appreciate your help Zephyrus, i’ve been trying this for days…!
i’ve set respawnOccurred to 0 at the top, is this what you mean?
var respawnOccurred : int = 0;
once i’ve killed the enemy or he’s out of range, and then subsequently back in range again, i want to re-initiate the function “spawnitems”.
i tried adding a boolean and while statement, but this continues to spawn non-stop, regardless if he’s dead/out of range it seems…
private var respawnedOnce = false;
EDIT: sorry, i added newer, longer code snippet below
function Update ()
{
// how far away is the player?
var distanceToPlayer = Vector3.Distance(transform.position, player.position);
// is he in range?
if (distanceToPlayer < spawnRange)
{
// in range. Do we have an active enemy and the player has just come into range, instantiate the prefab at our location.
if (!currentEnemy wasOutside)
if (respawnOccurred == 0){
currentEnemy = Instantiate(enemyPrefab, transform.position, transform.rotation);
respawnOccurred++;
return;}
respawnedOnce = true;
if (respawnOccurred >=1){
var counter=0;
while(respawnedOnce){
spawnitems();
counter++;
Debug.Log(counter);
if (counter >= respawnLimit)
return;
}
}
// if (respawnOccurred < respawnLimit respawnNumber > 0){
// spawnitems();
// }
// player is now inside our range, so set the flag to prevent repeatedly instantiating the prefab.
wasOutside = false;
}
// player is out of range.
else
{
// is player leaving the sphere of influence while our prefab is active?
if (currentEnemy !wasOutside){
Destroy(currentEnemy); // kill the prefab...
}
// ...and set our flag so we re-instantiate the prefab if the player returns.
wasOutside = true;
}
}
why not set it so when one of the spawned npcs is killed it subtracts 1 from respawnoccurred? Its kinda a hack, but if you are using it as a temporary solution it works
// is player leaving the sphere of influence while our prefab is active?
if (currentEnemy !wasOutside){
Destroy(currentEnemy); // kill the prefab...
}
Destroy(currentEnemy); when destroying you dont give a track record that it should reset, in such manner my tip will help you there, even though there is surely a much better solution ; )
The following code will always run regardless of wasOutside being true or false:
respawnedOnce = true;
if (respawnOccurred >=1){
var counter=0;
while(respawnedOnce){
spawnitems();
counter++;
Debug.Log(counter);
if (counter >= respawnLimit)
return;
}
}
I don’t think you intended it to run like that. By the way, your indenting is just awful. With that kind of indenting you don’t even know what’s going on half the time.
For example:
if(myVariable)
doSomething();
Should be:
if(myVariable)
doSomething();
If you do not know what I mean, please look for some code examples and fix your indenting. It will be much easier to spot errors.
thanks so much for the responses…! yes unfortunately im still awful at programming in general, i’ll definitely try out the suggestions and improve my indenting!