i am making a FPS multiplayer game. i don’t know how to make one player take damage when a different player shoots at them. could someone please help?
server i am using: nodejs socket server.
Are you using raycast or physical shooting
raycast i think. it uses a script, not a physical entity.
ok, then here
if(result.entity.name == 'other player')
{
result.entity.script.enemy.TakeDamage(this.damage);
}
thanks i will try it. i put this in the shooting script right?
its not working.
here is my shooting script:
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'});
// initialize code called once per entity
Shoot.prototype.initialize = function() {
this.currentAmmo = 0;
this.currentAmmo = this.maxAmmo;
this.canShoot = true;
};
// 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.entity.name == 'Other')
{
result.entity.script.enemy.TakeDamage(this.damage);
}
this.particleSys.particlesystem.play();
this.particleSys.particlesystem.reset();
}
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;
};
is this from @wasd1234?
if this is i think i may know it well enough
no it is from Quantumgames from project Left 2 Die
https://playcanvas.com/project/770449/overview/battle-legends-season-0
i did get the script from that person though yes
ok thanks i will try it
he already has the part i told him about in there already, the code right there from wasd1234 already had the code i told him about in there, unless he deleted it, so he jusy needs to change the script names and collision entity name
it is in the if result but it won’t work. it does not give an error, but it does not take away health from the other player
Has the entity the name ‘Other’?
that is the multiplayer placeholder entity yes
Placeholder? If you change the name by script after initialize it won’t work.
no what i mean is this:
the multiplayer server is from the real time multiplayer playcanvas tutorial. i don’t know exactly what “other” is, but i think it is the other player from multiplayer.
my new script is this
var RaycastShooting = pc.createScript('raycastShooting');
RaycastShooting.attributes.add('Ammo', {type : 'number', default : 30});
RaycastShooting.attributes.add('range', {type : 'number', default : 30});
RaycastShooting.attributes.add('text', {type : 'entity'});
RaycastShooting.attributes.add('debugColor1', {type : 'rgba'});
RaycastShooting.attributes.add('gunPoint', {type : 'entity'});
RaycastShooting.attributes.add('hitImpulse', {type : 'number', default : 10});
RaycastShooting.attributes.add('camera', {type : 'entity'});
// initialize code called once per entity
RaycastShooting.prototype.initialize = function() {
this.currentAmmo = 0;
this.currentAmmo = this.Ammo;
};
// update code called every frame
RaycastShooting.prototype.update = function(dt) {
var canShoot = true;
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);
var nextFire = 0;
if(this.currentAmmo === 0)
{
canShoot = false;
}
else
{
canShoot = true;
}
if(this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT) && canShoot === true)
{
if(result)
{
result.entity.collision.entity.rigidbody.applyImpulse(result.normal.scale(-this.hitImpulse));
}
this.currentAmmo -= 1;
this.app.renderLine(from, to, this.debugColor1);
}
if(result.entity.name == 'Other')
{
result.entity.script.Other.TakeDamage(this.damage);
}
if(this.currentAmmo < this.Ammo && this.app.keyboard.wasPressed(pc.KEY_R))
{
this.currentAmmo += (this.Ammo - this.currentAmmo);
}
it results as an error:
[raycastshoot.js?id=56781704&branchId=0ca9042a-e9f0-45cb-9b34-dfcbe75a537f:57]: Cannot read property 'enemy' of undefined
TypeError: Cannot read property 'enemy' of undefined
at scriptType.RaycastShooting.update (https://launch.playcanvas.com/api/assets/files/Scripts/gun%20scripts/raycastshoot.js?id=56781704&branchId=0ca9042a-e9f0-45cb-9b34-dfcbe75a537f:57:34)
My point is that other also is a player that use the same script I guess.