[SOLVED] Dynamic ridigbody slow down randomly

Hello

I have enemy follower type of script attached to enemy entity which supposed to follow player smoothly… but it slow down randomly

Note: Green is player i am controlling and red is enemy.

Check time stamp at 12 seconds, 28 seconds, 45 seconds

But according to above video, enemy is slowing down randomly while moving…

Enemy properties

Following script i am using

var EnemyController = pc.createScript('enemyController');

EnemyController.attributes.add('gamePlayManagerEntity', { type: 'entity' });
EnemyController.attributes.add('modelEntity', { type: 'entity' });

EnemyController.attributes.add('healthVal', { type: 'number', default: 100 });
EnemyController.attributes.add('movementSpeedVal', { type: 'number', default: 100 });


EnemyController.prototype.initialize = function () {
    this.playerEntity = null;
};

EnemyController.prototype.postInitialize = function () {
    this.playerEntity = this.gamePlayManagerEntity.script.gamePlayManagerScript.playerEntity;
};


EnemyController.prototype.update = function (dt) {
        this.updateEnemyPosition();
};


EnemyController.prototype.updateEnemyPosition = function () {

    this.entity.lookAt(this.playerEntity.getPosition());

    var entityPos = this.entity.getPosition();
    var entityRot = this.entity.getEulerAngles();
    this.entity.rigidbody.teleport(entityPos.x, entityPos.y, entityPos.z, entityRot.x, entityRot.y, entityRot.z);

    this.entity.rigidbody.applyForce(this.entity.forward.scale(this.movementSpeedVal));

};


You are teleporting and applying force, which is not the right way. You should do one or the other, not both.

Hello @LeXXik, Thanks for reply

Well, How can i make it took towards entity and then apply force… Where i want dynamic object as well which can be push backwards if required…

Use vector math to find the normalized direction vector from the enemy to player. Multiply that vector by force power and apply the resulting vector as a force on the rigidbody.

1 Like

Do mean something like this?

EnemyController.prototype.updateEnemyPosition = function () {
    
    this.entity.lookAt(this.playerEntity.getPosition());    // look towards player not working

    // Get the current positions of the enemy and the player
    var enemyPos = this.entity.getPosition();
    var playerPos = this.playerEntity.getPosition();

    // Calculate the direction vector from the enemy to the player
    var direction = new pc.Vec3();
    direction.sub2(playerPos, enemyPos);

    // Normalize the direction vector
    direction.normalize();

    // Scale the normalized direction by the movement speed
    var force = direction.scale(this.movementSpeedVal);

    // Apply the resulting force to the enemy's rigidbody
    this.entity.rigidbody.applyForce(force);
};

Enemy moves toward player smoothly without any trouble
But what about look towards player?
image

Make the model a child of the entity with the rigidbody component, so it can rotate freely. Then just rotate the visual model, not the rigidbody entity.

1 Like

image

@LeXXik
Solved!
Thank you very much for help! :slightly_smiling_face: :+1:

1 Like