I was looking for the best way to calculate velocity without it doubling when heading in a diagonal direction? For example when I go straight velocity is 20 but when I walk diagonally its 30
This is what I have so far:
var PlayFootsteps = pc.createScript('playFootsteps');
// initialize code called once per entity
PlayFootsteps.prototype.initialize = function () {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
this.entity.collision.on('collisionend', this.onCollisionEnd, this);
var walking = false;
var running = false;
};
var velocity = 0
// update code called every frame
PlayFootsteps.prototype.update = function(dt) {
velocity = this.roundToTwo(this.entity.rigidbody.linearVelocity.x) + this.roundToTwo(this.entity.rigidbody.linearVelocity.z)
/*
if (onground == true) {
if (!this.entity.sound.slot('walk').isPlaying) {
if(this.entity.sound != null) {
this.entity.sound.play("walk");
}
}
} else {
//this.entity.sound.stop('walk')
}
*/
};
PlayFootsteps.prototype.roundToTwo = function(num) {
return +(Math.round(num + "e+2") + "e-2");
}
// swap method called for script hot-reloading
// inherit your script state here
// PlayFootsteps.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/