Hi guys! Currently working on a basic project making enemy AI that follows you at a certain distance and decreases health when colliding. Although I enabled rigid body & collisions to the walls, for some reason the enemy goes through them is there a way to fix this? Thank you!!
https://playcanvas.com/editor/scene/1975718
Hi @jcatalan!
You can’t move a dynamic rigidbody using translate
. It will only move the entity and not the rigidbody of it. Instead you need to apply a force on the rigidbody of the entity.
ohh, so how would the code look when using force instead? because in the entity column of the enemy I added a dynamic rigid body. Should somewhere in this code be changed instead, at the end? thank you again!
var EnemyAi = pc.createScript('enemyAi');
EnemyAi.attributes.add('enemySpeed', {type: 'number', default: 1.0});
EnemyAi.attributes.add('detectionRange', {type: 'number', default: 5});
EnemyAi.attributes.add('playerEntity', {type: 'entity'});
// initialize code called once per entity
EnemyAi.prototype.initialize = function() {
};
// update code called every frame
EnemyAi.prototype.update = function(dt) {
// 1. get the player's position
var playerPosition = this.playerEntity.getPosition();
// 2. calculate the distance to player
var distanceToPlayer = this.entity.getPosition().distance(playerPosition);
// 3. check if the player is within the detection range
if(distanceToPlayer < this.detectionRange){
// 4. move towards the player
var direction = playerPosition.clone().sub(this.entity.getPosition()).normalize();
this.entity.translate(direction.scale(this.enemySpeed * dt));
}
};
You can try to replace line 24 with something like below.
this.entity.rigidbody.applyForce(direction.scale(this.enemySpeed * dt));
Hi! tried it right now and suddenly the enemies don’t follow the players anymore :// any way to fix this? thanks so much! PlayCanvas | HTML5 Game Engine
var EnemyAi = pc.createScript('enemyAi');
EnemyAi.attributes.add('enemySpeed', {type: 'number', default: 1.0});
EnemyAi.attributes.add('detectionRange', {type: 'number', default: 5});
EnemyAi.attributes.add('playerEntity', {type: 'entity'});
// initialize code called once per entity
EnemyAi.prototype.initialize = function() {
};
// update code called every frame
EnemyAi.prototype.update = function(dt) {
// 1. get the player's position
var playerPosition = this.playerEntity.getPosition();
// 2. calculate the distance to player
var distanceToPlayer = this.entity.getPosition().distance(playerPosition);
// 3. check if the player is within the detection range
if(distanceToPlayer < this.detectionRange){
// 4. move towards the player
var direction = playerPosition.clone().sub(this.entity.getPosition()).normalize();
this.entity.rigidbody.applyForce(direction.scale(this.enemySpeed * dt));
};
};
Sorry, I don’t know what is the reason.
I suggest to search on the forum or look at example project to see how you need to do it exactly.
oh okay, thank you so much anyways! means a lot
for anyone experiencing the same problem, i found the solution: change it to linear velocity:)this.entity.rigidbody.linearVelocity = directionToPlayer.scale(this.enemySpeed);