I would like to make enemies that has the ability to teleport across the map randomly (while they following you). How exactly would I do that? I already have the character set up to follow the player, so all I need is the teleporting part.
var target : Transform;
var vel_vec : Vector3;
var speed : float;
var distance_byte : float;
var player_life : Player_life_script;
function Update()
{
vel_vec = target.position - transform.position; // creates the vector from the enemy to you.
transform.LookAt(target); // enemy looks to you
transform.Translate(Vector3.forward * speed * Time.deltaTime); // enemy walks to you
if (vel_vec.magnitude <= distance_byte) //if is near
{
animation.Play("byte", PlayMode.StopAll);
player_life.hp -= 1;
vel_vec = Vector3.zero;
}
else if (vel_vec.magnitude > distance_byte + 5) // if is far
animation.Play("walk", PlayMode.StopAll);
}
If they are beyond a certain distance you can teleport or catch them up by setting the enemy/npc position to the player position. Maybe use a random amount away so they don’t spawn right on top of the player.
Get the distance to the other player from the enemy/npc maybe in Update:
var vel_vec : Vector3;
var speed : float;
var distance_byte : float;
var player_life : Player_life_script;
var close :boolean;
var following:boolean;
var spawnTimer:int;
var spawn:boolean;
var timer:float;
var fader:float;
var fadeOut:boolean;
var fadeIn:boolean;
var target : Transform;
var pos:Vector3[]=new Vector3[5]; //Here you need to fill the array with some various vector3
function Start(){
pos[0]=new Vector3(x,y,x); //Here intead of x,y,z put the appropriate positions
pos[1]=new Vector3(x,y,x);
pos[2]=new Vector3(x,y,x);
pos[3]=new Vector3(x,y,x);
pos[4]=new Vector3(x,y,x);
target = GameObject.Find("Player").transform;
}
function Update(){
if(close){
following = true;
vel_vec = target.position - transform.position; // creates the vector from the enemy to you.
transform.LookAt(target); // enemy looks to you
transform.Translate(Vector3.forward * speed * Time.deltaTime); // enemy walks to you
if (vel_vec.magnitude <= distance_byte) //if is near{
animation.Play("byte", PlayMode.StopAll);
player_life.hp -= 1;
vel_vec = Vector3.zero;
}else if (vel_vec.magnitude > distance_byte + 5) // if is far
animation.Play("walk", PlayMode.StopAll);
}else
// Add codes that gets your Alien from a waypoint to another randomly to create an effect of wandering.
if(following){
timer += Time.deltaTime;
if(!spawn){ // Get a random int for defining when the nPC is teleporting
spawnTimer = Random.Range(1,6);
spawn = false;
}
if(timer>spawnTimer){ // Check if the timer is greater than the random value
fadeOut = true;
}
if (fadeOut)FadingOut();
else if (fadeIn) FadingIn();
}
}
function FadingOut(){
renderer.material.color.a-=Time.deltaTime; // The NPC slowly disappear
if(renderer.material.color.a < 0.1){ // If almost gone
transform.position = FindClosestTarget();//Here calling function
fadeOut = false;
fadeIn = true;
}
}
function FadingIn(){
renderer.material.color.a+=Time.deltaTime; //NPC appears slowly
if(renderer.material.color.a>0.99){ // If NPC is fully in
fadeIn = false; // All var set back to initial
timer = 0.0;
spawn = false;
following = false; // The NPC is then not following anymore
}
}
function FindClosestTarget () : Vector3 {
var closest : Vector3;
var distance = Mathf.Infinity;
for (var i:int = 0; i < 5; i ++ ) { //Iterating through all target and defining the closest to the target/player
var diff = (target.position - pos*);*
var curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = pos*;* distance = curDistance; } } return closest; } function OnTriggerEnter(other:Collider){ if(other.gameObject.tag == “Player”) close = true; } function OnTriggerExit(other:Collider){ if(other.gameObject.tag == “Player”) close = false; } Ok I added your part of the script. You need to add a sphere collider set to Is Trigger to your alien. When your player steps inside the wone the alien will cahse him. While chasing the following script will trigger until you manage to either kill him or run away out of the sphere zone.You still need to add the part where your alien is wandering around when is not chasing you.