Help with gun logic telport and duplicate

Hi, I am working on a gun logic script. I have this already, this is just to see if you can shoot and reload.

var Gunlogic = pc.createScript('gunlogic');
var maxammo = 100;
var curruntammo = maxammo;
// initialize code called once per entity
Gunlogic.prototype.initialize = function() {

};

// update code called every frame
Gunlogic.prototype.update = function(dt) {
if(this.app.keyboard(pc.KEY_R)){
    curruntammo = maxammo;
}
if(this.app.keyboard(pc.MOUSEBUTTON_MIDDLE, pc.MOUSEBUTTON_LEFT, pc.MOUSEBUTTON_RIGHT)){
    if(curruntammo > 1){
curruntammo -= 1;
    }
    if(curruntammo < 1){
        
    }
}
if(curruntammo < 0 ){
    curruntammo = 1;
};

};

This code just needs to acess a bullet, teleport it to the gun and shoot it, please help!
EDIT- here is my game - PlayCanvas | HTML5 Game Engine

@Codeknight999 What type of bullet action are you looking for? Will you use physics or will you be using the ray casting method? Here is an example project that uses ray casting to handle this kind of action.

https://playcanvas.com/project/990658/overview/weapontest

1 Like

Hi @Tirk182! What is a ray cast? And would physics just use apply impulse?

@Codeknight999 To answer the second question yes, applying an impulse to a projectile or cloned object. The raycast method is the most commonly used in FPS games. It is more or less a invisible line draw between the player and an object in the scene. Normally it’s used when pressing the left mouse button to fire at an object. The returned results provide a good amount of information about the entity hit as long as that entity has collision enabled. In the example project I have linked above you can see it in action inside gun-shoot.js. For more information you can have a look to these references.

https://developer.playcanvas.com/en/tutorials/?tags=raycast

1 Like

Thanks again @Tirk182 ! I will look at the link you provided and get back to you.

1 Like