It seems you are directly setting the position of a dynamic rigidbody, which won’t work. Instead, replace this.bullet.setPosition(gp); with this.bullet.rigidbody.teleport(gp);
The issue here is that var gp = this.guPoint.getPosition(); returns you a reference to the pc.Vec3 rather than a copy. This means that gp is always representing the current position of the guPoint. What you have intended to have is a copy:
Shoot.prototype.shoot = function(){
// Get a copy of the position as a new pc.Vec3 object
var gp = this.guPoint.getPosition().clone();
this.bullet.rigidbody.applyImpulse(0,5,0);
// the .bind ensures that we take the instance of this script as the
// context of 'this' in the function
setTimeout(function(){
this.bullet.setPosition(gp);
}.bind(this),1000);
};
However, what I think you really want to do here is disable the bullet after a second as passed otherwise the bullet will just be floating in midair:
Shoot.prototype.shoot = function(){
var gp = this.guPoint.getPosition();
this.bullet.rigidbody.teleport(gp);
this.bullet.enabled = true;
this.bullet.rigidbody.applyImpulse(0,5,0);
// the .bind ensures that we take the instance of this script as the
// context of 'this' in the function
setTimeout(function(){
this.bullet.enabled = false;
}.bind(this),1000);
};
I think what he is trying to do is to have the bullet respawn back to it’s original spot before it was shot after a certain amount of time has passed after being shot.
var Shoot = pc.createScript('shoot');
Shoot.attributes.add('guPoint', {type: 'entity'});
Shoot.attributes.add('bullet', { type: 'entity' });
// initialize code called once per entity
Shoot.prototype.initialize = function() {
};
// update code called every frame
Shoot.prototype.update = function(dt) {
// Press X to shoot
if(this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT)) {
this.shoot();
}
};
Shoot.prototype.shoot = function(){
var gp = this.guPoint.getPosition();
this.bullet.translateLocal(0,0,-6);
var goBack = this.bullet.rigidbody.teleport(gp);
setTimeout(function(){
var goBack = this.bullet.rigidbody.teleport(gp);
},1000);
};
// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/