- Home /
Make enemy appear in front of player and then disappear at random times
I am making a horror game and was wondering if it was possible to make an enemy appear in front of a player (x amount of meters away), and then disappear after a matter of seconds. When the enemy disappears. I was hoping i would not have to use triggers because I want it to be completely random.
Answer by CharlesD · Aug 15, 2012 at 07:10 AM
This seems like the perfect place to be using a croutine. Coroutines have little overhead and can be run independent of the inherited methods like "Update" and "FixedUpdate." The C# script below when attached to your enemy will reveal him, then make him disappear a random amount of time later, note that this script will not check if the enemy is colliding with something or being occluded, for that you will need to add some sort of check.
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public float distanceToPlayer = 3F;
public float minTimeInView = 1F;
public float maxTimeInView = 1F;
private Transform cam;
public void Spawn () {
StartCoroutine ( RandomEncounter () );
}
void Start () {
//Get Camera Transform
cam = Camera.mainCamera.transform;
//Hide
renderer.enabled = false;
Spawn ();
}
private IEnumerator RandomEncounter () {
//Adding another yeild statement like the one below here will cause a random delay before spawning
//Show Again
renderer.enabled = true;
//Set the position in front of the player
Vector3 pos = cam.forward;
pos *= distanceToPlayer;
pos += cam.position;
transform.position = pos;
//Wait A Random Interval Of Time Before Disapearing
yield return new WaitForSeconds (Random.Range (minTimeInView, maxTimeInView));
//Hide Again
renderer.enabled = false;
//If the enemy has a collider attached move it somewhere where it won't be in the way or destroy it here
}
}
Does he only teleport once to you, or does he continuously do it over a period of time?
how to make enemy teleport to you after 45 sec. or something?
If you look in the RandomEncounter script you will see a line saying "Adding another yield statement like the one below here will cause a random delay before spawning" just add a line like
yield return new WaitForSeconds (45);
at that commonet.
Whenever I Use It It Comes After 45 Seconds But Wont Dissapear Again? Help But I Dont Want To Destroy $$anonymous$$y Ghost I Just Want It To Repeatedly Appear/Dissapear
For making it appear and disappear more than once you will need to encase everything in the RandomEncounter with a true while loop (just putting true as the condition is good enough). If the ghost won's disappear then that means he does not have a renderer component attached, so for the code at this line
"renderer.enabled = false;"
you will ins$$anonymous$$d have to teleport your ghost somewhere where it can't be seen like beneath the map. I highly recommend you research the function of the code with Unity's script reference page, it will pay off in the long run.
Answer by djfunkey · Oct 05, 2012 at 07:58 AM
public Transform ScaryMonster;
public Transform ScaredPlayer;
public int FirstAppear = 0;
private int MonsterStayTime = 1;
public int Random1;
public int Random2;
private Random RandomTime;
void Update ()
{
RandomTime = Random.Range(Random1, Random2);
FirstAppear += Time.time;
if (FirstAppear = 20)
{
Transform scarymonster;
scarymonster = Instantiate (ScaryMonster, ScaredPlayer.position, scarymonster.LookAt(ScaredPlayer)) as Transform;
Destroy (ScaryMonster, MonsterStayTime);
Random1 = 30;
Random2 = 40;
}
if (RandomTime <= Time.time)
{
scarymonster = Instantiate (ScaryMonster, ScaredPlayer.position, scarymonster.LookAt(ScaredPlayer)) as Transform;
Destroy (ScaryMonster, MonsterStayTime);
Random1 = 80;
Random2 = 90;
}
if (RandomTime <= Time.time)
{
scarymonster = Instantiate (ScaryMonster, ScaredPlayer.position, scarymonster.LookAt(ScaredPlayer)) as Transform;
Destroy (ScaryMonster, MonsterStayTime);
Random1 = 120;
Random2 = 130;
}
/* you get the drift, you just have to copy the above code multiple times for how ever many times you want */
}
Your answer
Follow this Question
Related Questions
How to make enemy teleport right next to player? 4 Answers
Make an enemy appear and then disappear at random? 1 Answer
Player damage stops working over time 0 Answers
Hurt Player if close to Enemy! Need Help 4 Answers
Attack within 0.61 meters? 1 Answer