• 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 Kantus · Jan 19, 2015 at 12:52 PM · pathfindingrespawndeleteassign

Conflict between Pathfinding & Respawning

Hi all,

I'm currently developing my first Unity project, in 2D, and I'm running into a little problem. I have a script (thanks, Brackeys!) that detects when the player has fallen to their death, w$$anonymous$$ch respawns them after a set amount of time. I also have an enemy, with an A* pathfinding script that has a variable called TARGET, and uses t$$anonymous$$s to plot the enemy's pathfinding towards the player. The conflict, however, arises between these two scripts - the respawn script is only called after the player object has been destroyed, and Instantiates a new player object at the spawnPoint - but then the pathfinding script bugs out, because the Player it was trying to path to has been deleted!

So, how exactly does one go about re-setting TARGET in the pathfinding script when the respawn script Instantiates a new player?

Thanks for your help.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Jan 19, 2015 at 01:28 PM

I suggest t$$anonymous$$s little workaround: Beside the target variable, have another "backup" variable called targetPos w$$anonymous$$ch holds a Vector3. When the Player (the target) is destroyed, the targetPos is assigned the player't positions just before he dies.

In the AI script, whenever you try to access the target variable to get it's transform.position, you also check if target isn't null (because the object has been destroyed)., and if it is, then you use the targetPos variable instead to determine where the AI should move to.

T$$anonymous$$s has the added benefit of letting your AI characters move to any Vector3 position in the world, not only to an object's transform.position. So you can make patrol paths or random movement just by assigning the targetPos variable instead.

I would even go so far and t$$anonymous$$nk about if you can eliminate the target variable altogether and only use Vector3s for pat$$anonymous$$ng targets.

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 chariot · Jan 19, 2015 at 01:37 PM

If u have only one player u can try to find with tag in Update or make some conditions for finding it. For ex in pathfinding script. Also u need to send "dead" bool to t$$anonymous$$s script before destroing player, and then bump find new object.

 private var playerObj : GameObject;
 private var bump : boolean;
 
 function Start (){
     playerObj = GameObject.FindGameObjectWithTag("Player");
 }
 
 function Update(){
     var playerScr : NameOfPlayerScript = playerObj.GetComponent(NameOfPlayerScript);
     if (playerScr.dead){
         bump = true;
     }
     else {
         bump = false;
     }
     if (bump){
         playerObj = GameObject.FindGameObjectWithTag("Player");
     }
 }

More simple (but expensive) way in Update () drop t$$anonymous$$s:

 playerObj = GameObject.FindGameObjectWithTag("Player");


And dont forget to set tag for player :)

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 Kantus · Jan 20, 2015 at 03:07 AM 0
Share

Thanks for the responses so far. I tried out chariot's way of doing this, and after many false starts it seems to work. Seems being the operative here, as I can't get the AI to function quite properly anyway, so it's hard to test if this particular problem has been fixed. This is the reason why I'm hesitant to thumbs-up this solution right now - because I don't know if it'll work out in the grand scheme of things, but it works for now. Thanks!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Update Graph after GameObject destruction 1 Answer

Horde of NavMeshAgents - stops to recalculate path. 4 Answers

AI pathfinding waypoints 1 Answer

A* pathfinding for 2D top Down 0 Answers

How to bake navmesh on walls? 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