View Single Post
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 08-23-2021 , 09:50   Re: Entity moving in front of player
Reply With Quote #4

Trace a line down from the player current origin and you'll get the "on ground" position.

Code:
new Float:start[3], Float:end[3]
pev(id, pev_origin, start)
end[0] = start[0]
end[1] = start[1]
end[2] = start[2] - 8192.0

// with IGNORE_MONSTERS, the trace will ignore players
// you may want to replace with DONT_IGNORE_MONSTERS
engfunc(EngFunc_TraceLine, start, end, IGNORE_MONSTERS, id, 0)

new Float:fraction
get_tr2(0, TR_flFraction, fraction)

if (fraction != 1.0)
	get_tr2(0, TR_vecEndPos, end)


// end[] is now the position on ground
// do something...
And to get the origin in front of the player:

Code:
new Float:start[3], Float:viewOfs[3]

pev(id, pev_origin, start)
pev(id, pev_view_ofs, viewOfs)
start[0] += viewOfs[0]
start[1] += viewOfs[1]
start[2] += viewOfs[2]

new Float:velocity[3], Float:end[3]

// replace 250 with the desired distance
velocity_by_aim(id, 250, velocity)

end[0] = start[0] + velocity[0]
end[1] = start[1] + velocity[1]
end[2] = start[2] + velocity[2]

engfunc(EngFunc_TraceLine, start, end, IGNORE_MONSTERS, id, 0)

new Float:fraction
get_tr2(0, TR_flFraction, fraction)

if (fraction != 1.0)
	get_tr2(0, TR_vecEndPos, end)

// end[] is now the position in front of the player
// do something...
In both cases the entity may end up getting stuck. If that happens, get TR_vecPlaneNormal, multiply by scalar X, where X is an arbitrary given value, you'll have to test to determine what's the proper value, start with 15.0, then add the result to end[].
__________________









Last edited by CrazY.; 08-23-2021 at 10:05.
CrazY. is offline