I have a entity called Zombie which I need to chase the player, I got the movement to work, Zombie is using RigidBody and after a few searches I found it lookAt wont work on an entity with RigidBody, so I put Zombie’s model inside a parent entity which I made the parent entity have the RigidBody and do the movement, now for some reason, even inside a parent I can’t get lookAt to work on the Zombie model, here’s the code.
var ZombieAi = pc.createScript('zombieAi');
// initialize code called once per entity
ZombieAi.prototype.initialize = function() {
this.player = this.app.root.findByName('player');
this.maleArm = this.app.root.findByName('ZombieModel');
this.speed = 2; // adjust as needed
};
// update code called every frame
ZombieAi.prototype.update = function(dt) {
var direction = this.player.getPosition().clone().sub(this.entity.getPosition());
direction.y = 0; // don't move up or down
direction.normalize();
var eulerAngleX = this.maleArm.getEulerAngles().x;
var eulerAngleY = this.maleArm.getEulerAngles().y;
var eulerAngleZ = this.maleArm.getEulerAngles().z;
this.maleArm.lookAt(this.player.getPosition());
this.maleArm.setEulerAngles(90, this.maleArm.getEulerAngles().y, this.maleArm.getEulerAngles().z);
// this.entity.lookAt(this.player.getPosition());
// move the entity towards the player
this.entity.rigidbody.applyImpulse(direction.scale(this.speed));
};
This script is attached to the parent which contains the ZombieModel