I’m making a platformer and need help with making an animation that makes the platform go back and forth.
I am assuming that you need your player to be able to jump and run on the platform and not just have the platform go back and forth without it being walkable? This would mean that the platform would have a collision component and a rigid body component so that it can interact with your character.
In that case you would need to add a script component on the platform and add a script like so:
var Platform = pc.createScript('platform');
// initialize code called once per entity
Platform.prototype.initialize = function() {
this.speed = 1; // speed of movement
this.duration = 2; // duration of movement
this.timer = 0; // timer used to reverse speed
this.direction = new pc.Vec3();
};
// update code called every frame
Platform.prototype.update = function(dt) {
// increase timer
this.timer += dt;
// if timer greater than duration reverse speed
if (this.timer > this.duration) {
this.timer = 0;
this.speed = -this.speed;
}
// move entity right / left
var position = this.entity.getPosition();
this.direction.copy(this.entity.right).scale(this.speed * dt);
position.add(this.direction);
// if this has a rigid body we need to use the 'teleport' method otherwise
// we could use this.entity.setPosition(position)
this.entity.rigidbody.teleport(position);
};
you can also do this by using “tween” to interpolate between two positions. Then you can place empty entities on the two points you want the platform to move to, and make a script where you have attributes for those entities. then use the “tween” script from playcanvas (https://github.com/playcanvas/playcanvas-tween) and use the positions of those entities to set the “from” and “to” values. then use “loop” and “yoyo”. short and simple solution