I have made the gun, the crosshair, and all of that, I even added it, I just need a script for shooting and Hopefully, a reload. I want a pistol with 12 rounds and that cant shoot while reload. How do i do that (it will reset on reload)?
Have you thought about how the bullet should move? Personally, I find it easiest to use an entity with a script, a collision component and a kinematic rigidbody.
i want just a yellow ball moving forward at the in the center of the crosshair.
If youve ever played my game, its all simple, so having complicated gun structure is gonna be not fitting with the game https://playcanvas.com/project/769132/overview/capsule-boy
Let’s start making the bullet. When you have it ready, show what it looks like.
so I just make a yellow circle? Where? in front of the gun?
Hey @thebosser24,
If that’s what you want then yeah!
Yes, it can be a child of the gun entity. We disable the bullet entity when it’s ready.
i did that
now what
and i disabled it
Give the the bullet the name “Bullet”, and be sure it has all the components (script, collider and kinematic rigidbody). Also create a script with the name “Bullet”, replace the code with the code below and add the script to the script component.
var Bullet = pc.createScript('Bullet');
// initialize code called once per entity
Bullet.prototype.initialize = function() {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
this.entity.collision.on('collisionend', this.onCollisionEnd, this);
setTimeout(function(){
this.entity.destroy();
}.bind(this), 3000);
};
// update code called every frame
Bullet.prototype.update = function(dt) {
this.entity.translateLocal(0,0,-0.2);
};
Bullet.prototype.onCollisionStart = function (result) {
if (result.other) {
this.entity.destroy();
}
};
Yes Sir
do you want Collider? or collistion
i did it, but the stuff isnt work, i also disabled it
You are not finished yet. Coding is not easy and takes a lot of time. How do you want to fire the bullet?
for this pistol i want it to be semi automatic, so on click it fires 1 on hold it fires 1
I suggest to give your crosshair entity actually the name “Crosshair”. We need this for the next step. I advise you to give all entities a good name because otherwise you will lose the overview very quickly.
ive been, my team dont do that well
Hey @thebosser24,
Here is a code that can help but just change it
var Shoot = pc.createScript('shoot');
// Shoot.attributes.add('maxAmmo', {type : 'number', default : 30});
// Shoot.attributes.add('range', {type : 'number', default : 30});
// Shoot.attributes.add('text', {type : 'entity'});
// Shoot.attributes.add('text2', {type : 'entity'});
// Shoot.attributes.add('particleSys', {type : 'entity'});
// Shoot.attributes.add('debugColor1', {type : 'rgba'});
// Shoot.attributes.add('gunPoint', {type : 'entity'});
// Shoot.attributes.add('hitImpulse', {type : 'number', default : 10});
// Shoot.attributes.add('damage', {type : 'number', default : 10});
// Shoot.attributes.add('fireRate', {type : 'number', default : 10});
// Shoot.attributes.add('camera', {type : 'entity'});
// Shoot.attributes.add('automatic', {type : 'boolean'});
//
// initialize code called once per entity
Shoot.prototype.initialize = function() {
this.weapon = "Pistol";
};
// update code called every frame
Shoot.prototype.update = function(dt) {
if(this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT) && this.weapon == "Pistol"){
this.Shoot({par:this.app.root.findByName(this.weapon),spd:0.5,spos:this.app.root.findByName("Bullet_Hole").getPosition()});
}
var cam = this.app.root.findByName("Camera");
var weps = this.app.root.findByName("Player").findByName("Weapons");
if(this.app.keyboard.isPressed(pc.KEY_SHIFT) && this.weapon !== "flashlight"){
weps.setLocalPosition(weps.getLocalPosition().lerp(weps.getLocalPosition(),new pc.Vec3(-0.25,0,0),0.5));
cam.camera.fov = pc.math.lerp(cam.camera.fov,30,pc.math.smootherstep(0,1,0.25));
}else{
weps.setLocalPosition(weps.getLocalPosition().lerp(weps.getLocalPosition(),pc.Vec3.ZERO,0.5));
cam.camera.fov = pc.math.lerp(cam.camera.fov,45,pc.math.smootherstep(0,1,0.25));
}
};
Shoot.prototype.Shoot = function(options) {
options = options || {};
var bullet = new pc.Entity();
bullet.addComponent("model");
bullet.addComponent("collision");
bullet.collision.type = "sphere";
bullet.collision.radius = 0.25/2;
bullet.model.type = "asset";
bullet.model.asset = 43068333;
var position = options.spos;
bullet.setPosition(position);
bullet.setLocalScale(0.15,0.15,0.15);
bullet.setRotation(this.app.root.findByName("Camera").getRotation());
this.app.root.addChild(bullet);
bullet.collision.once("triggerenter",function(f){
if(f && f.name === "Zombie") f.script.enemy.hp -= 4;
},this);
setInterval(function(){
bullet.translateLocal(0,0,-options.spd);
});
};
Please recheck the name of your crosshair. Forgetting a capital will result in an error. After that you have to add some code to your player script. I need to know the exact name of the script that controls the player.
Movement.js
and there is a capital C in Crosshair, @Gabriel_Dobrzynski where do I put the script
Yello?
In the initialize function of your player Movement.js script you have to add the code below (if you don’t have something like this already).
this.rounds = 12;
if (this.app.mouse) {
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
this.app.mouse.on(pc.EVENT_MOUSEUP, this.onMouseUp, this);
this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
}
In the same script you have to add the code below as a new function (if you don’t have this function already).
Movement.prototype.onMouseDown = function(event) {
if (event.button === pc.MOUSEBUTTON_LEFT) {
// shoot
if (this.rounds !== 0) {
var bullet = this.app.root.findByName('Bullet').clone();
this.app.root.addChild(bullet);
bullet.setPosition(this.app.root.findByName('Bullet').getPosition());
bullet.lookAt(this.app.root.findByName('Crosshair').getPosition());
bullet.rotateLocal(0, 180, 0);
bullet.enabled = true;
// update magazine
this.rounds = this.rounds - 1;
}
}
if (event.button === pc.MOUSEBUTTON_RIGHT) {
// reload magazine
this.rounds = 12;
}
};
This code is not tested. If you get an error, please let me know.
Because you don’t have added the first part of the code on the correct place in your script. You have to add it inside your initialize function.