Can't change position of object pooling Enemies that are using Navigation Mesh
Hello creators!
I have object pooling going on for anything that requires spawning. The idea is I would spawn x amount of that item out of the player's view. When I want the object to "spawn" I would move it to the location I want . This is working for items that aren't moving
However, when I try to apply the same logic to my enemy, my Enemy would not move to the correct spot. It seems to be conflicting with my navmesh code in my update. If I commented out my Update code, the enemy would move to the correct spot.
Here is my Update function attached to my enemy
onUpdate(deltaTime: number)
{
this.bounceTime += deltaTime;
const bounceOffset = Math.sin(this.bounceTime * this.props.bounceFrequency! * 2 * Math.PI) * this.props.bounceAmplitude!;
this.entity.position.set(this.entity.position.get().add(new Vec3(0, bounceOffset, 0)));
const playerPosition = this.currentTarget?.position.get();
if (playerPosition) {
const point = this.navmesh?.getNearestPoint(playerPosition, 10);
if (playerPosition && playerPosition.distance(this.entity.position.get()) < 10) {
} else {
}
if (point) {
this.agent?.destination.set(point);
// console.log("hi" + this.player?.name.get())
const playerDirection = playerPosition.sub(this.entity.position.get()).normalize();
const lookDirection = playerPosition;
this.entity?.lookAt(lookDirection);
}
}
}In my object pooling script, I'm setting its position when I need it to "spawn", but this isn't working for my enemy that is using navmesh.
obj.position.set(position);
As a side note that may or may not be related: The "bouncing" code you see in the Update() isn't working either unless I remove the navmesh code which starts after if(playerPosition). So I know what's causing positions not moving but I don't understand why or how I could solve it
So it turns out the reason is the position are conflicting. In Update I'm am setting the enemies' position, but in Object pool I'm also setting its position.
So in Update I added a boolean to check if position should be locked. If it's locked then I can move the enemy. If not locked then the enemy is executing the update function
if (!this.isPositionLocked) { const playerPosition = this.currentTarget?.position.get(); if (playerPosition) { const point = this.navmesh?.getNearestPoint(playerPosition, 10); if (playerPosition && playerPosition.distance(this.entity.position.get()) < 10) { } else { } if (point) { this.agent?.destination.set(point); // console.log("hi" + this.player?.name.get()) const playerDirection = playerPosition.sub(this.entity.position.get()).normalize(); const lookDirection = playerPosition; this.entity?.lookAt(lookDirection); } } }