Hello everyone, I am trying to create a laser weapon. I can create an effect line or use laserbeam.spr, but I want to use TE_TRACER. I set the start of the tracer as the player’s position and tried to convert the player’s view angle to a vector for the end, but I failed. I tried many functions, even Ent_Add_AimingVec in the entlib include, but it never worked the way I wanted.
I am leaving the codes of the plugin below. You may see many absurd codes or unnecessary methods, I am a novice in this matter. You can improve every stage of this plugin, I leave it to you.
Right now, the only thing I need is for the tracer to work properly.
Thanks in advance.
Code:
#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <hamsandwich>
#include <fakemeta>
#include <fakemeta_util>
#include <fakemeta_stocks>
#include <xs>
#include <engine>
#include <fun>
#define VectorMA(%0,%1,%2,%3) ( %3[0] = %0[0] + %1 * %2[0], %3[1] = %0[1] + %1 * %2[1], %3[2] = %0[2] + %1 * %2[2] )
new bool:isLaserActive[MAX_PLAYERS+1];
new laserOrigin[32][3]
// ... geri kalan kodunuz
public plugin_init()
{
register_clcmd("+laser","activateLaser")
register_clcmd("-laser","deactivateLaser")
RegisterHam(Ham_Spawn, "player", "fwHamPlayerSpawnPost", 1);
}
public fwHamPlayerSpawnPost(iPlayer) {
if (is_user_alive(iPlayer)) {
strip_user_weapons(iPlayer)
}
}
public activateLaser(playerId)
{
if( !is_user_alive(playerId) )
return PLUGIN_HANDLED
get_user_origin(playerId,laserOrigin[playerId],3)
isLaserActive[playerId] = true
drawLaserBeam(playerId)
return PLUGIN_HANDLED
}
public deactivateLaser(playerId)
{
if( !is_user_alive(playerId) )
return PLUGIN_HANDLED
isLaserActive[playerId] = false
removeLaserBeam(playerId)
return PLUGIN_HANDLED
}
public client_PreThink(playerId)
{
if(!is_user_alive(playerId))
{
removeLaserBeam(playerId);
return PLUGIN_CONTINUE;
}
if(isLaserActive[playerId])
{
get_user_origin(playerId,laserOrigin[playerId],3); // continuously update player's position
removeLaserBeam(playerId); // remove old laser beam
drawLaserBeam(playerId); // draw new laser beam
// Check if the laser beam hits an enemy player
new aimedPlayerId, bodyPart;
get_user_aiming(playerId, aimedPlayerId, bodyPart);
if(is_user_alive(aimedPlayerId) && (cs_get_user_team(playerId) != cs_get_user_team(aimedPlayerId)))
{
// Apply fixed damage to the enemy player
new damage = (bodyPart == 1) ? 9500 : 1; // If headshot, damage is 2. Else, damage is 1.
if (damage == 9500){
fm_fakedamage(aimedPlayerId, "", float(damage), DMG_BLAST);
effecths(aimedPlayerId)
}else{
fm_fakedamage(aimedPlayerId, "", float(damage), DMG_BULLET);
}
// Create a spark at the point of impact
new Float:aimOrigin[3], Float:angles[3], Float:dir[3];
pev(playerId, pev_v_angle, angles);
xs_anglevectors(angles, dir, Float:{0.0, 0.0, 0.0}, Float:{0.0, 0.0, 0.0});
VectorMA(laserOrigin[playerId], 0.1, dir, aimOrigin);
new trace;
engfunc(EngFunc_TraceLine, laserOrigin[playerId], aimOrigin, DONT_IGNORE_MONSTERS, playerId, trace);
get_tr2(trace, TR_vecEndPos, aimOrigin);
Sparks(aimOrigin);
}
}
return PLUGIN_CONTINUE;
}
public Sparks(Float:Origin[3])
{
message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
write_byte(TE_ARMOR_RICOCHET)
engfunc(EngFunc_WriteCoord, Origin[0])
engfunc(EngFunc_WriteCoord, Origin[1])
engfunc(EngFunc_WriteCoord, Origin[2])
write_byte(2)
message_end()
}
public removeLaserBeam(playerId)
{
message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
write_byte(99) // TE_KILLBEAM
write_short(playerId)
message_end()
return true; // laser beam removed
}
public effecths(id)
{
static Float:origin[3]
pev(id, pev_origin, origin)
engfunc(EngFunc_MessageBegin, MSG_PVS, SVC_TEMPENTITY, origin, 0)
write_byte(TE_IMPLOSION) // TE ID
engfunc(EngFunc_WriteCoord, origin[0])
engfunc(EngFunc_WriteCoord, origin[1])
engfunc(EngFunc_WriteCoord, origin[2] + 20.0)
write_byte(50) // Radius
write_byte(50) // Count
write_byte(1) // Duration
message_end()
}
public drawLaserBeam(playerId)
{
new origin[3], end[3];
get_user_origin(playerId, origin, 3);
// coding angels vectors end
// Lazeri çizin
message_begin(MSG_BROADCAST, SVC_TEMPENTITY);
write_byte(6); // TE_TRACER
write_coord(origin[0]);
write_coord(origin[1]);
write_coord(origin[2]);
write_coord(end[0]);
write_coord(end[1]);
write_coord(end[2]);
message_end();
}