I’m trying my enemy (Hulk) to look at me and follow me. I’m able to do it using the lookAt function, translateLocal and RotateLocal functions, However, the enemy, even though it’s a rigidbody, does not collide with other rigidbody objects. When I try to use the applyForce codes, it only moves on the world Axis and the lookAt function does not work anymore. I tried using the teleport and setLocalPosition codes but it does not seem to work. Attached are the code below, including all the options that I’ve tried which have been commented. Thanks
var HulkFollowsPlayer = pc.createScript('hulkFollowsPlayer');
// initialize code called once per entity
HulkFollowsPlayer.prototype.initialize = function() {
this.thePlayer = this.app.root.findByName("Player");
};
// update code called every frame
HulkFollowsPlayer.prototype.update = function(dt) {
/*
//OPTION 1 WITH BUG
//hulk should lookat the Player
this.entity.lookAt(this.thePlayer.getPosition());
this.entity.rotateLocal(0,180,0);
this.entity.translateLocal(0,0,.03);
//hulk enemy just to follow the x and z postition
var EnemyPosition = this.entity.getPosition();
//change or set the local Y position of hulk to a constant value
this.entity.setLocalPosition(EnemyPosition.x, 0, EnemyPosition.z);
//------------------*/
//OPTION 2 EXPERIMENT
this.entity.lookAt(this.thePlayer.getPosition());
this.entity.rotateLocal(0,180,0);
this.entity.translateLocal(0,0,.03)
// this.entity.setLocalEulerAngles(0,180,0);
//var force = this.entity.forward.clone().scale(-3);
// this.entity.rigidbody.applyForce(force);
//this.entity.rigidbody.linearVelocity(0,0,3);
//this.force.copy(this.entity.forward).scale(-3);
// this.entity.translateLocal(0,0,.03);
//hulk enemy just to follow the x and z postition
//var EnemyRotation = this.entity.getLocalR
//this.entity.rigidbody.applyForce(0,0,3);
var EnemyPosition = this.entity.getLocalPosition();
//change or set the local Y position of hulk to a constant value
//this.entity.setLocalPosition(EnemyPosition.x, 0, EnemyPosition.z + .03);
//this.entity.setLocalPosition(EnemyPosition.x, 0, EnemyPosition.z + .03);
//this.entity.rigidbody.teleport(EnemyPosition.x, EnemyPosition.y, EnemyPosition.z + .03);
};
// swap method called for script hot-reloading
// inherit your script state here
// HulkFollowsPlayer.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/
With these methods you only move the visual entity and not the dynamic rigidbody of it. Thats why there is no collision anymore at the position where you expect it after using one of these methods.
You basically only can move or rotate an entity with a dynamic rigidbody using teleport(), applying a force or manipulate the rigidbody directly on another way.
I see you currently don’t use a dynamic rigidbody for your enemy entity. Be aware there is no automatic interaction with other objects when you use a kinematic rigidbody. You need to detect and react on collision by script manually.
I’ve already set it to Dynamic before and using the teleport function. But it still does not work. I’ve tried using getLocalPosition and getPosition but it still does not my work.
Here’s the new code. As you can see in the previous code, I’ve tried all options even if my enemy is set to a dynamic rigidbody :
var HulkFollowsPlayer = pc.createScript('hulkFollowsPlayer');
// initialize code called once per entity
HulkFollowsPlayer.prototype.initialize = function() {
this.thePlayer = this.app.root.findByName("Player");
};
// update code called every frame
HulkFollowsPlayer.prototype.update = function(dt) {
/*
//OPTION 1 WITH BUG
//hulk should lookat the Player
this.entity.lookAt(this.thePlayer.getPosition());
this.entity.rotateLocal(0,180,0);
this.entity.translateLocal(0,0,.03);
//hulk enemy just to follow the x and z postition
var EnemyPosition = this.entity.getPosition();
//change or set the local Y position of hulk to a constant value
this.entity.setLocalPosition(EnemyPosition.x, 0, EnemyPosition.z);
//------------------*/
//OPTION 2 EXPERIMENT
this.entity.lookAt(this.thePlayer.getPosition());
this.entity.rotateLocal(0,180,0);
//this.entity.translateLocal(0,0,.03)
// this.entity.setLocalEulerAngles(0,180,0);
//var force = this.entity.forward.clone().scale(-3);
// this.entity.rigidbody.applyForce(force);
//this.entity.rigidbody.linearVelocity(0,0,3);
//this.force.copy(this.entity.forward).scale(-3);
// this.entity.translateLocal(0,0,.03);
//hulk enemy just to follow the x and z postition
//var EnemyRotation = this.entity.getLocalR
//this.entity.rigidbody.applyForce(0,0,3);
var EnemyPosition = this.entity.getLocalPosition();
//change or set the local Y position of hulk to a constant value
//this.entity.setLocalPosition(EnemyPosition.x, 0, EnemyPosition.z + .03);
//this.entity.setLocalPosition(EnemyPosition.x, 0, EnemyPosition.z + .03);
this.entity.rigidbody.teleport(EnemyPosition.x, EnemyPosition.y, EnemyPosition.z + .03);
};
// swap method called for script hot-reloading
// inherit your script state here
// HulkFollowsPlayer.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/
Hopefully you can fork the project and help me with the codes. Thanks :))
Yes the problem is on line 65. I want Hulk to come toward me (the Player)…but it’s moving on the world axis not on the local Z axis…the same thing happens if I use the applyForce method
As for line 35…I just want hulk to face toward me :))
I already tried making it a negative value…(-0.3) but it still doesn’t work. The enemy does not come toward me. So does that mean that the LookAt function is not compatible with a dynamic rigidbody so that the enemy is continuously looking at my first person controller Player and following me?? :((