- Home /
How to make enemy tp to you after 45 sec.?
How to make my enemy in this game teleport to me after 45 sec. or something? I used this code, to make him teleport to me:
using UnityEngine; using System.Collections;
public class Enemy : MonoBehaviour {
public float distanceToPlayer = 5F;
public float minTimeInView = 1F;
public float maxTimeInView = 1F;
private Transform cam;
public void Spawn () {
StartCoroutine ( RandomEncounter () );
}
void Start () {
cam = Camera.mainCamera.transform;
renderer.enabled = false;
Spawn ();
}
private IEnumerator RandomEncounter () {
renderer.enabled = true;
Vector3 pos = cam.forward;
pos *= distanceToPlayer;
pos += cam.position;
transform.position = pos;
yield return new WaitForSeconds (Random.Range (minTimeInView, maxTimeInView));
renderer.enabled = false;
}
}
In your start function you could set an Invoke("TeleportToPlayer", 45); and then in that function do a quick check to see if the enemy is already at the player's position before doing the actual teleport.
Where excacly to put: Invoke ("TeleportToPlayer", 45); ?
Answer by aldonaletto · Sep 16, 2012 at 06:06 PM
This code seems ok - are you having any problem? Anyway, disabling the renderer only makes the enemy invisible - it may attack the player, or the player can collide with it. Is this an intended behaviour? Another point: disabling the renderer can leave childed objects still visible - if the enemy has a weapon childed to it, you may have a "haunted weapon" effect, where it moves by itself and attacks the player!
If you want to make the object completely invisible, including any children, use something like this:
... Renderer[] rendrs; // holds a list of renderers in the object and children
void SetVisible(bool onOff){ // sets all of them visible/invisible, according to onOff: foreach (Renderer rendr in rendrs) rendr.enabled = onOff; }
void Start () { cam = Camera.mainCamera.transform; rendrs = GetComponentsInChildren< Renderer>(); SetVisible(false);
Spawn (); }
private IEnumerator RandomEncounter () { SetVisible(true); Vector3 pos = cam.forward; pos *= distanceToPlayer; pos += cam.position; transform.position = pos; yield return new WaitForSeconds (Random.Range (minTimeInView, maxTimeInView)); SetVisible(false); } }
Actually, i want him to spawn in front of me, then disappear after 2-4 seconds, and then later appear again then disappear.....
This is C#, like the script in the question - actually, this code replaces the original from void Start() to the end.
@blackpantherxx, you can do the whole job inside Start:
... public float $$anonymous$$TimeHidden = 5.5f; public float maxTimeHidden = 15.0f; public bool showEnemy = true;
IEnumerator Start () { cam = Camera.mainCamera.transform; rendrs = GetComponentsInChildren< Renderer>(); while (true){ // endless loop SetVisible(false); // hide enemy and wait random time:
yield return new WaitForSeconds (Random.Range($$anonymous$$TimeHidden, maxTimeHidden)); // clear showEnemy to hide the bastard forever: while (showEnemy == false) yield return 0; // bring enemy in front of player: Vector3 pos = cam.forward; pos *= distanceToPlayer; pos += cam.position; transform.position = pos; // and make it visible for a random time: SetVisible(true); yield return new WaitForSeconds (Random.Range ($$anonymous$$TimeInView, maxTimeInView)); } }
This code provides a way to control the enemy: when showEnemy is true, the enemy appears and vanishes at random intervals; when showEnemy is false, the enemy stops appearing.
Your answer
Follow this Question
Related Questions
Trap Door Question 1 Answer
how to kill an enemy 1 Answer
Damaging enemies... 1 Answer
Enemy Health Help 1 Answer
A node in a childnode? 1 Answer