Hi guys, a have a problem with smooth transfering my entity to the exact position.
I have chess 8x8 field. A player entities can travel between these squares, but they must stop at the 0, 0, 0 point of the square the are travelling to.
Here’s the project
You have to have 2 players at the battlefield to be able to move. Active player is blinking and have movement area highlighted.
Network script call player script moveTo function:
Player.prototype.moveTo = function(point) {
this.targetPoint = point;
this.direction = new pc.Vec3();
this.direction.sub2(this.targetPoint, this.entity.localPosition);
this.direction.normalize();
this.distance = utils.getDistanceBetweenTwoPoints(this.entity.localPosition, this.targetPoint);
};
Player update function has the following code:
if (this.distance > this.approachRange) {
if (this.state != 'run') {
this.setState('run');
}
var move = this.direction.clone();
move.scale(this.movingSpeed * dt);
this.entity.translate(move);
this.entity.position.lerp(this.entity.position, this.direction, dt);
this.distance = utils.getDistanceBetweenTwoPoints(this.entity.position, this.targetPoint);
} else {
if (this.state == 'run') {
var direction = new pc.Vec3();
direction.sub2(this.targetPoint, this.entity.localPosition);
this.entity.translate(direction);
this.entity.position.lerp(this.entity.position, this.direction, dt);
this.distance = 0;
this.setState('idle');
this.entity.fire('endMovement');
}
}
So, what’s the problem?
I test the project with 2 browsers at the same time. And it may work a little slowly. It seems sometimes i have a situation, when in one frame i have this.distance > 0.5 (the entity hasn’t reached the target point yet) and in the next frame i also have this.distance > 0.5 (the entity has already passed the target point).
As the result the entity keeps moving and never stops.
So, i have multiple questions:
- How to avoid the situations i described above?
- Perhaps there’s a better solution for a movement code?
- Why there’s such performance issues with the project? There’re only 2 models and a plane (battleground).
Thanks in advance