PLEASE HELP!
my multiplayer game is not working. when i shoot at another player, i get killed, not the other player. can someone please help me fix this? my project: https://playcanvas.com/editor/scene/1263394
Hi @quantum,
Can you share your exact code that replicates the shooting behavior to the connected players?
raycast.js
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('camera', {type : 'entity'});
Shoot.attributes.add('enemy', {type : 'entity'});
// initialize code called once per entity
Shoot.prototype.initialize = function() {
this.currentAmmo = 0;
this.currentAmmo = this.maxAmmo;
this.canShoot = true;
this.damage = 10;
};
// update code called every frame
Shoot.prototype.update = function(dt) {
var from = this.gunPoint.getPosition();
var to = this.camera.camera.screenToWorld(screen.width / 2 , screen.height / 2 - 37, this.range);
var result = this.app.systems.rigidbody.raycastFirst(from, to);
if(this.currentAmmo === 0)
{
this.canShoot = false;
}
else
{
this.canShoot = true;
}
if(this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT) && this.canShoot === true)
{
if(result)
{
result.entity.collision.entity.rigidbody.applyImpulse(this.camera.forward.scale(this.hitImpulse), result.normal);
this.currentAmmo -= 1;
}else
{
return;
}
if (result && result.entity.name === "RemotePlayer" && "LocalPlayer") {
result.entity.script.health2.takeDamage(10);
}
this.particleSys.particlesystem.play();
this.particleSys.particlesystem.reset();
}
if(this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT) && this.entity.enabled === "Sword")
{
if(result)
{
result.entity.collision.entity.rigidbody.applyImpulse(this.camera.forward.scale(this.hitImpulse), result.normal);
}else
{
return;
}
if (result && result.entity.name === "RemotePlayer") {
result.entity.script.health2.takeDamage(5);
}
}
if(this.app.keyboard.wasPressed(pc.KEY_R))
{
this.currentAmmo = this.currentAmmo + (this.maxAmmo - this.currentAmmo);
}
this.text.element.text = this.currentAmmo;
this.text2.element.text = '/ ' + this.maxAmmo;
};
That handles shooting locally only.
You will have to network this to your connected players.
um ok… i am new to this so how would i do that?
You said you are already having multiplayer working in your game, only shooting is not working I gather?
How are you doing multiplayer now?
Start by studying this tutorial on how to network your game:
https://developer.playcanvas.com/en/tutorials/real-time-multiplayer/
no i already have the exact smae script from this tutorial in my game i just need to know how to make the shooting work
Sorry I can’t help you on that.
In your place I would study how that scripts works and replicates movement to other clients, and I will do the same for shooting.