Enemy Appear script help.

So i have this script and for some reason I dont know what to set the appear rate nd the distance from player too. I also dont know how to make it so that the enemy re-appears when you walk away from it. If your confused on what I am saying then heres a basic rundown. I want it so that when the enemy dissappears as you walk away, it will re-appear later around you sort of like slenderman. Hope this clears ome confusion.

Heres the code I have:

var player : Transform;      // the Object the player is controlling
var spawnOrgin : Vector3;     // this will be the bottom right corner of a square we will use as the spawn area
var maximum : Vector3;        // max distance in the x, y, and z direction the enemy can spawn
var spawnRate : float;        // how often the enemy will respawn
var distanceToPlayer : float; // how close the enemy has to be to the player to play music 

private var nearPlayer : boolean = false; // use this to stop the teleporting if near the player
private var nextTeleport : float = 0.0f; // will keep track of when we to teleport next

function Start ()
{
    nextTeleport = spawnRate;
}

function Update ()
{
    if (!nearPlayer)     // only teleport if we are not close to the player
        {
                if (Time.time > nextTeleport)   // only teleport if enough time has passed
                {            
                
                transform.position = Vector3( Random.Range(spawnOrgin.x, maximum.x), Random.Range(spawnOrgin.y, maximum.y), Random.Range(spawnOrgin.z, maximum.z) );   // teleport            
                
                nextTeleport += spawnRate;    // update the next time to teleport        
                    }    
                
                }
                
                if (Vector3.Distance(transform.position, player.position) <= distanceToPlayer)    
                {        
                if (audio && audio.clip && !audio.isPlaying)     // play the audio if it isn't playing              
                audio.Play();         
                
                nearPlayer = true;
                
                }    
                else    
                
                {         
                
                if (audio)            
                audio.Stop();
                
                nearPlayer = false;
                    }

anyone got any help for me?

So howwould i do that? I suck at coding :\

–

@leggod You are welcome. Do note the update re using Coroutines with Parse.com API... if thats what you are doing that keeps things simpler and more in the Unity style. Loom and SceneLoom are definitely useful where you need to do your own threaded dispatching.

–

1 Answer

1

The logic to do what you want is almost all here. After you’ve attached this script look in the inspector. ‘spawnRate’ is the amount of time in seconds between potential appearances. ‘distanceToPlayer’ is the distance that separated near from far. There is no code here that flips nearPlayer from true back to false, but you’ve not pasted all the code. As for making him appear around the player rather in in a set location, you will need to modify this line of code.

transform.position = Vector3( Random.Range(spawnOrgin.x, maximum.x), Random.Range(spawnOrgin.y, maximum.y), Random.Range(spawnOrgin.z, maximum.z) );   // teleport  

Rather than be spawned inside a rectangular area, I would recommend using Random.insideUnityCircle along with player position as the starting point.