- Home /
How to make an enemy teleport?
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);
}
Answer by fafase · Jul 31, 2012 at 10:16 AM
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[i]);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = pos[i];
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.
I am getting a few errors when I put your code in, fafase. This is what i get:
Assets/Scripts/teleport script.js(23,10): BCE0044: expecting (, found 'FadingOut'.
Assets/Scripts/teleport script.js(23,21): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Scripts/teleport script.js(24,29): BCE0044: expecting :, found '-='.
How would I fix this? Thanks!
Now it is telling me this:
Assets/Scripts/teleporting.js(27,40): BCE0005: $$anonymous$$ identifier: 'x'.
Assets/Scripts/teleporting.js(27,42): BCE0005: $$anonymous$$ identifier: 'y'.
Assets/Scripts/teleporting.js(27,44): BCE0005: $$anonymous$$ identifier: 'z'.
Ok the pb here is I do not know how your level looks like. You need to generate some values here that fit your environment. I would for instance recommend an array of Vector3 and you get an index randomly (I will update the script). You need to find position on your map where the NPC would not appear half way through something or in a tree, or a wall, you get the idea.
so will this script allow the enemy to teleport near me (no matter where i am on the map? or just spawn in one location?
Let's say you have 5 positions around the map that you have entered in the array because if chosen by the generator, the nPC would not be in a weird situation, then this script will take the NPC from behind you and spawn it at one of those locations. Then from there I guess you have another mecanic that takes him towards the player and when close enough, the NPC is following you so the boolean following is true and this process goes again. Is taht what you need?
Answer by drawcode · Jul 31, 2012 at 06:02 AM
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:
float distance = Vector3.Distance (playerObject.transform.position, npcObject.transform.position);
If over a certain amount
if(distance > 20f) {
Vector3 playerPosRandomized= playerObject.transform.position;
// randomize x or z
playerPosRandomized.x = playerPosRandomized.x + UnityEngine.Random.Range(-10f,10f);
playerPosRandomized.z = playerPosRandomized.z + UnityEngine.Random.Range(-10f,10f);
npcObject.transform.position = playerPosRandomized;
// play some effect
}
If you need teleport effects make a particle effect appear or a sprite animation when updating the position.
Answer by jjj51 · Aug 03, 2012 at 05:17 PM
I couldn't get it to work either, but good luck. And you aren't trying to recreate Slender are you? ;) haha
$$anonymous$$ore of a scary alien FPS, with this script it should turn out pretty cool. I don't know what slender is.
Your answer
Follow this Question
Related Questions
How to make Enemy on scary game ? 1 Answer
switch player and npc follow player 0 Answers
Navmesh follow and stop at a distance 2 Answers
Enemy backing away from player after going around corner? 1 Answer
Enemy reorienting for player 1 Answer