Hi, i’m loosing my mind, i have this code, were the movement works, but the lookAt don’t both use this.targetPosition but in the log i added it returns 0,0,0 though the movement works. am i going crazy?
Enemy.prototype.update = function(dt) {
if (!this.entity || this.state === "dead") return;
if (!this.targetPosition) return;
const pos = this.entity.getPosition();
const dir = new pc.Vec3();
dir.sub2(this.targetPosition, pos);
dir.y = 0;
const distance = dir.length();
if (distance > 0.05) {
// Normalizza direzione e scala per velocità e dt
const speed = this.enemySpeed || 1;
const velocity = dir.normalize().scale(speed);
// Applica la velocità al rigidbody
if (this.entity.rigidbody) {
this.entity.rigidbody.linearVelocity = velocity;
} else {
pos.add(velocity.scale(dt));
this.entity.setPosition(pos);
}
// Guarda verso la destinazione
this.entity.lookAt(this.targetPosition);
console.log("lookat",this.targetPosition)
// Animazione camminata
const animName = enemyAnimations.walk[this.enemy];
if (this.currentAnim !== animName) {
this.model.animation.play(animName, this.blendTime);
this.model.animation.loop = true;
let animSpeed = 1;
if (this.enemy === 4 || this.enemy === 9 || this.enemy === 17) animSpeed = 2;
else if (this.enemy === 7 || this.enemy === 11 || this.enemy === 12 || this.enemy === 13) animSpeed = 0.8;
else if (this.enemy === 6) animSpeed = 0.5;
else if (this.enemy === 0 || this.enemy === 1 || this.enemy === 2 || this.enemy === 3 || this.enemy === 5) animSpeed = 0.3;
this.model.animation.speed = animSpeed;
this.currentAnim = animName;
}
this.state = "walking";
} else {
// Arrivo a destinazione
this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
this.state = "idle";
// Animazione idle
this.idle();
console.log(`Enemy ${this.enemyId} arrived at destination`);
window.NetworkColyseusInstance.sendEnemyReached(this.enemyId, { x: pos.x, y: pos.y, z: pos.z });
this.targetPosition = new pc.Vec3();
}
};
project here https://playcanvas.com/project/674858/overview/the-5-seals-mmorpg
Just found out the random destination code broke so target is always 0,0,0 …but the entity look at a different place anyway. Investigating.