- Home /
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;
}
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.
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.
I still have not found a solution to this issue, any help would be great.
thanks in advance.
Your answer
Follow this Question
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