Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
6
Question by jach5270 · Sep 29, 2014 at 10:55 AM · positionspawnnavmeshagentteleport

Navmesh agent teleporting

I've run into a rather irritating issue with my AI enemies. what's happening is that in the single frame after spawning, some (and only some) enemies will instantly change their position.

ive made sure that they are spawning within the nav mesh, and that the position is definitely not being set in code. it seems like the problem is coming from the navmesh agents path finding, as if they are trying to resume their old path even though ive definitely called both Stop() and ResetPath() and even giving them new targets upon spawning.

i assume that the solution is something obvious, and im simply using the nav mesh system wrong. any help would be greatly appreciated.

here is a screenshot of the scenario: alt text

also here is the Spawn() and Kill() methods that are used for resetting the enemies. very little of it is relevant though.

 void Kill(){
         //variable resets
         rigidbody.mass = 10;
         dead = true;
         canMove = false;
         targeting = false;
         targetArrow.renderer.enabled = false;
         mesh.renderer.materials = new Material[] {deadMat, deadMat};
         track1.emissionRate = 0;
         track2.emissionRate = 0;
         CameraManager.Shake(20f, 20);
         if(navMesh.enabled){
             navMesh.ResetPath();
             navMesh.Stop();
         }
 
         //death effects
         if(Network.isServer){
             Network.Instantiate(deathExplosion, transform.position, Quaternion.identity, 0);
             GM.MM.networkView.RPC("SetDead", RPCMode.All, new Vector3(Stg.Get<int>("index", settings), 1, 0));
         }
         else{
             Instantiate(deathExplosion, transform.position, Quaternion.identity);
             Instantiate(deathExplosion2, transform.position, Quaternion.identity);
         }
 
         //versus extra
         if(Stg.mode == 1){
             Stg.IncreaseStat("kills", 1, damageOwner.stats);
             Stg.IncreaseStat("score", 10, damageOwner.stats);
             Stg.IncreaseStat("deaths", 1, stats);
             HUDManager.HM.hudM.showKillInfo(Stg.GetString("name", settings) + " was killed by " + Stg.GetString("name", damageOwner.settings));
             respawnReset = Stg.Get<float>("respawnTime", Stg.gameS);
             
             if(Stg.Get<int>("index", settings) == GM.masterPlayerIndex){
                 GM.cam.SetTarget(Stg.Get<int>("index", damageOwner.settings));
                 GM.hud.hudM.showDeathScreen = true;
             }
         }
 
         //survival extra
         if(Stg.Get<bool>("useMobDensity", Stg.gameS)){
             GM.mobDensity -= Stg.Get<int>("densityValue", settings);
         }
         if(Stg.mode == 2){
             GM.SrM.waveProgress += Stg.Get<int>("waveProgressValue", settings);
         }
     }
 
     void Spawn(){
         //variable resets
         respawnReset = -10;
         moveSpeedChange = 1;
         rigidbody.mass = 1;
         canFire = true;
         dead = false;
         canMove = true;
         canDamage = true;
         if(Stg.Get<bool>("useMaxHealth", settings))
             Stg.Set("maxHealth", "" + Stg.Get<int>("startingHealth", Stg.gameS), settings);
         health = Stg.Get<int>("maxHealth", settings) * Stg.Get<float>("healthModifier", Stg.gameS);
         maxHealth = health;
         SetMaterials();
 
         if(navMesh.enabled){
             navMesh.ResetPath();
             navMesh.Resume();
         }
 
         //versus extra
         transform.position = GM.GetSpawn(settings);
         if(Stg.Get<int>("index", settings) == GM.masterPlayerIndex){
             GM.cam.SetTarget(Stg.Get<int>("index", settings));
             GM.hud.hudM.showDeathScreen = false;
         }
 
         //survival extra
         if(Stg.Get<bool>("useMobDensity", Stg.gameS)){
             GM.mobDensity += Stg.Get<int>("densityValue", settings);
         }
 
         //effects
         Instantiate(spawnEff, transform.position + Vector3.up*1, Quaternion.identity);
     }
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 TeamOutbreak · Feb 08, 2016 at 02:11 PM 0
Share

I had the exact same problem. As jakethehuman mentioned, using 'Warp()' fixed the issue for me.

Example:

     public void teleportTo(Vector3 point) {
         //Change the position
         transform.position = point;
 
         // Warp should always be used when a Nav$$anonymous$$eshAgent is moved to keep them in-sync
         if (GetComponent<AICharacterControl>()) {
             GetComponent<AICharacterControl>().agent.Warp(point);
         }
     }


From what I found, the agent was updating the position with very little regards to what you set it to. 'Warp' appears to keep the agent informed as to what you want.

2 Replies

· Add your reply
  • Sort: 
avatar image
10

Answer by jakethehuman · Dec 15, 2015 at 09:49 PM

You can do that or use Warp().

Comment
Add comment · Show 2 · 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 jcaguiat · Oct 23, 2017 at 02:49 PM 1
Share

This is the best solution for teleporting while using a nav mesh.

avatar image softrare · Dec 05, 2017 at 03:40 PM 0
Share

Warp is the correct solution. The other one did not work for me.

avatar image
8

Answer by Curb · Sep 29, 2014 at 11:48 AM

I had a similar issue not too long ago.. What worked for me was making sure to turn off the gameobject before moving it, even if you're instancing the object and positioning it in the same frame. In hindsight, it would probably also work by disabling the agent component, or perhaps even by setting agent.updateposition to false before moving..

As to why that worked for me my guess would be that when you're instancing an object it spawns it at world coordinate zero, and then tries to move it to the proper position, but because the agent is active during the positioning, it'll have trouble getting across a gap and get stuck somewhere.

Comment
Add comment · Show 3 · 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 scnoobi · Sep 29, 2014 at 12:43 PM 0
Share

disabling the navmesh component works aswell like this guy said and then reenable after a certain time.

avatar image jach5270 · Sep 29, 2014 at 10:10 PM 0
Share

thank you! disabling the nav$$anonymous$$esh agent and then using a coroutine to enable a few frames later seems to have worked.

on a side note, what would be the best way to temporarily pause enemies on a path? e.g. i need my enemies to stay frozen for a couple of seconds when a round stars, and then again when the round is over (similar to games like TF2 or any CoD). what would be the best way of doing this?

avatar image Davide-Barbieri jach5270 · Dec 16, 2015 at 11:16 AM 0
Share

if you move enemies with nav$$anonymous$$eshAgent you can do something like at the begin of the round: agent.Stop(); yield WaitForSeconds(number_of_second_before_begin); agent.Resume();

if you use simple moving with a speed variable you can do in script: speed=0; yield WaitForSeconds(number_of_second_before_begin); speed=15 (for example)

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

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

Related Questions

Spawning System 1 Answer

Navmesh After Spawn 2 Answers

Navmesh problem with SpawnSystem 0 Answers

Nav Mesh problem~ I need help! 0 Answers

NavMeshAgent stops when player is in inaccesible area 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