How do I switch animations in scripts 1.0? I really do not like the new system plus, the project was created before 2.0 came out and the api reference only shows 2.0.
The animation API is exactly the same in both scripting systems. To switch to a new animation on an entity, do:
this.entity.animation.play('myanim.json', blendTime);
Why don’t you like the new system?
I just had some problems initially and change is not something I usually like. I know the old scripting engine.
So, I have a script set up, but it’s not doing what I need it to.
pc.script.create('input', function (app) {
var a = 0;
var SPEED = 5;
var input = function (entity) {
this.entity = entity;
};
input.prototype = {
// Called once after all resources are loaded and before the first update
initialize: function () {
this.speed = 5;
this.movement = new pc.Vec3();
},
// Called every frame, dt is time in seconds since last update
update: function (dt) {
if (app.keyboard.isPressed(pc.KEY_A)) {
this.entity.rotateLocal(0, a + 1, 0);
} else if (app.keyboard.isPressed(pc.KEY_D)) {
this.entity.rotateLocal(0, a - 1, 0);
}
if (app.keyboard.isPressed(pc.KEY_S)) {
this.movement.copy(this.entity.forward).scale(this.speed * dt);
this.entity.translate(this.movement);
} else if (app.keyboard.isPressed(pc.KEY_W)) {
this.entity.animation.play('swagger_walk_inPlace.json', 0.1);
this.movement.copy(this.entity.forward).scale(-1 * this.speed * dt);
this.entity.translate(this.movement);
}
if (app.keyboard.isPressed(pc.KEY_SHIFT)) {
this.speed = 10;
} else {
this.speed = 5;
}
}
};
return input;
});
I need it to play the animation while the w key is pressed, as soon as it’s pressed. I played around with the blend time, but it still doesn’t work right.
The problem here is that, while the W key is pressed, your update function will, every frame, call this.entity.animation.play
. This causes the animation to play the first frame of the animation over and over again. What you want is the following:
if (app.keyboard.isPressed(pc.KEY_W)) {
if (!this.animationPlaying) {
this.entity.animation.play('swagger_walk_inPlace.json', 0.1);
this.entity.animation.speed = 1;
this.animationPlaying = true;
}
} else {
if (this.animationPlaying) {
this.entity.animation.speed = 0;
this.animationPlaying = false;
}
}
```
Obviously, you have to declare animationPlaying in your initialize function:
```
this.animationPlaying = false;
```
Ok, but when I let go the animation doesn’t switch back. I tried replaceing the program in the else if with the program in the if, but it gave me the same problem as before.
Well, let’s say you had another animation which is an idle animation. It would be:
if (app.keyboard.isPressed(pc.KEY_W)) {
if (!this.walking) {
this.entity.animation.play('swagger_walk_inPlace.json', 0.1);
this.walking = true;
}
} else {
if (this.walking) {
this.entity.animation.play('idle.json', 0.1);
this.walking = false;
}
}
Works great! But how do I make it not loop the animation for jumping?