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 blackpantherxx · Sep 16, 2012 at 03:27 PM · enemyteleporthow

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;
         
          }
 
 }

Comment
Add comment · Show 3
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 blackpantherxx · Sep 16, 2012 at 02:00 PM 0
Share

created in C#

avatar image wilco64256 · Sep 16, 2012 at 05:18 PM 0
Share

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.

avatar image blackpantherxx · Sep 17, 2012 at 12:44 PM 0
Share

Where excacly to put: Invoke ("TeleportToPlayer", 45); ?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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); } }

Comment
Add comment · Show 4 · 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 blackpantherxx · Sep 17, 2012 at 01:08 PM 0
Share

Actually, i want him to spawn in front of me, then disappear after 2-4 seconds, and then later appear again then disappear.....

avatar image Skyline Studios · Sep 17, 2012 at 08:21 PM 0
Share

js/C# Which One Is It?

avatar image aldonaletto · Sep 18, 2012 at 01:25 AM 0
Share

This is C#, like the script in the question - actually, this code replaces the original from void Start() to the end.

avatar image aldonaletto · Sep 19, 2012 at 12:37 AM 0
Share

@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

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

13 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

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


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