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 JoeIkin · Jul 03, 2015 at 01:58 PM · navmeshfunctionclasses

a function os not correctly working on certain subclasses but does work on other subclasses with the same inherited class

So I'm working on a pseudo-RTS style game and have a problem where enemies will spawn in and use navmesh to navigate towards player placed buildings, all buildings types have their own sub-classes and inherit from a single building class. However enemies will not path towards certain building types. They are detecting them and setting them as their target but are not moving towards them. Please, any help would be greatly appreciated.

here is the code which the enemies use to select a target building and move towards it

 GameObject GetClosestBuilding()
     {
         GameObject bestTarget = null;
         float closestDistanceSqr = Mathf.Infinity;
         Vector3 currentPosition = this.transform.position;
 
         GameObject[] objectarray;
         objectarray = GameObject.FindGameObjectsWithTag("Building");
 
         if (objectarray != null)
         {
             foreach(GameObject potentialTarget in objectarray)
             {
                 Vector3 directionToTarget = potentialTarget.transform.position - currentPosition;
                 float dSqrToTarget = directionToTarget.sqrMagnitude;
             //    float priority = dSqrToTarget * potentialTarget.GetComponent<BuildingClass>().lightIntensity;
 
                 if (dSqrToTarget < closestDistanceSqr)
                 {
                     closestDistanceSqr = dSqrToTarget;
                     bestTarget = potentialTarget;
                 }
             }
         }
 
         float attackRangeSquared = (this.attackRange * this.attackRange);
 
         if (closestDistanceSqr <= attackRangeSquared)
         {
             this.inAttackRange = true;
         }
         else
         {
             this.inAttackRange = false;
         }
         return bestTarget;
     }
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 Owen-Reynolds · Jul 03, 2015 at 03:19 PM 0
Share

You write the "funny" buildings are being detected, and are being set as targets. So the code you posted, GetClosestBuilding, is working perfectly?

You write that the problem is the movement code? (which we can't see, and, anyway, if bestTarget is properly set, seems unlikely the move code will "sense" a certain target is for a bad building.) I'd guess the targets are being set wrong somewhere, or else some subclasses aren't using virtual, override, ... for functions.

avatar image JoeIkin · Jul 03, 2015 at 03:35 PM 0
Share

Thanks for your reply,

I believe that the GetClosestBuilding code is working correctly, here is the function used to move the enemies

void $$anonymous$$oveTo () { Nav$$anonymous$$eshAgent agent = GetComponent<Nav$$anonymous$$eshAgent>(); agent.destination = target.transform.position; agent.stoppingDistance = target.GetComponent<Nav$$anonymous$$eshObstacle>().radius; }

I cannot see any problems here either, however I hoped that someone more experienced than myself might see something that I had missed.

i have double checked and there are no overrides in the subclasses, infact two of the subclasses are currently empty, one of which is correctly interracting with enemies and the other is not. I am finding this to be a very confusing issue, any further help would be greatly appreciated.

avatar image JoeIkin · Jul 07, 2015 at 12:13 PM 0
Share

I still have not found a solution to this issue, any help would be great.

thanks in advance.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

function isnt working in the class 0 Answers

Pacman ghost behaviour 0 Answers

Function argument referencing variables? 1 Answer

Instantiate inside class function returns NullReference 1 Answer

Unity C# Start function without extending Mono 2 Answers


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