Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mwoodington · Aug 02, 2012 at 09:03 PM · playerenemydisappearhorrorappear

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Loius · Aug 15, 2012 at 07:41 AM 1
Share

pop scaaaaaares fist shake

x)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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
  
     }
 
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cbjfan1 · Aug 24, 2012 at 10:56 PM 0
Share

Does he only teleport once to you, or does he continuously do it over a period of time?

avatar image blackpantherxx · Sep 16, 2012 at 01:55 PM 0
Share

how to make enemy teleport to you after 45 sec. or something?

avatar image CharlesD · Sep 18, 2012 at 08:24 PM 0
Share

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.

avatar image Skyline Studios · Sep 19, 2012 at 05:04 PM 0
Share

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

avatar image CharlesD · Sep 21, 2012 at 05:17 AM 0
Share

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.

avatar image
0

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 */
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges