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 phavington · Jul 31, 2012 at 05:07 AM · playercharacterenemyfollowteleport

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);
 }
Comment
Add comment
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

3 Replies

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

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.

Comment
Add comment · Show 19 · 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 phavington · Jul 31, 2012 at 06:44 PM 0
Share

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!

avatar image phavington · Jul 31, 2012 at 07:14 PM 0
Share

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'.

avatar image fafase · Jul 31, 2012 at 07:36 PM 0
Share

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.

avatar image phavington · Jul 31, 2012 at 07:53 PM 0
Share

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?

avatar image fafase · Jul 31, 2012 at 07:58 PM 0
Share

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?

Show more comments
avatar image
0

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.

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
avatar image
0

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

Comment
Add comment · Show 1 · 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 phavington · Aug 03, 2012 at 05:24 PM 0
Share

$$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

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

9 People are following this question.

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

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


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