Jump to content

Projectile To Crosshair


Recommended Posts

Good day, community.

I've tried getting our projectiles ( rocket_left and rocket_right ) to fire directly to the crosshair by manipulating it's velocity with the crosshair's on-screen positioning values. Then ran into some "vector" errors.

So I restored the code back to working / test functionality as seen below.

The question is, which would be the best way to achieve something like this? The screen positioning for the crosshair is a Vector2, while the projectile's velocity arguments require Vector3 if I'm not mistaken. I haven't really got an idea of how to work out the math on this. Quite a challenge, this one. So any bit of recommendations will be appreciated 👌 

Reasoning: Having the rockets focused to the exact crosshair point would allow for better accuracy when firing at targets close range, or where ever the crosshair hits.

 

-- mark them
function vCrosshair( )
	if (localPlayer.vehicle) then
		local matrix = localPlayer.vehicle.matrix
		local startPos = matrix:transformPosition( 0, 0, 0 )
		local endPos = matrix:transformPosition( 0, 200, 0 )
		local hit, x, y, z = processLineOfSight( startPos, endPos, true, true, true, false, false, false, false, true, localPlayer.vehicle )
		local hitPos = Vector3( x, y, z )
		local endPos = hit and hitPos or endPos
		local csrPos = Vector2( getScreenFromWorldPosition ( endPos ) )
			if (csrPos.length > 0) then 
				if (hit) then
					dxDrawText( "•", csrPos - Vector2 ( 1/2, 1/2), 1, 1, tocolor( 0, 255, 0, 255 ), 2, "default-bold" )
				else
					dxDrawText( "•", csrPos - Vector2 ( 1/2, 1/2), 1, 1, tocolor( 0, 255, 0 , 255 ), 2, "default-bold" )
			end
		end
	end
end
addEventHandler( "onClientRender", root, vCrosshair )


-- send the gifts from uncle sam
function vMissile( )
	
	local vehicle = localPlayer.vehicle
	if (vehicle) then

		local matrix = localPlayer.vehicle.matrix
		local startPos = matrix:transformPosition( 0, 0, 0 )
		local endPos = matrix:transformPosition( 0, 200, 0 )
		local hit, x, y, z = processLineOfSight( startPos, endPos, true, true, true, false, false, false, false, true, localPlayer.vehicle )
		local hitPos = Vector3( x, y, z )
		local endPos = hit and hitPos or endPos
		local csrPos = Vector2( getScreenFromWorldPosition ( endPos ) )

		local rx, ry, rz = rotation
		local vx, vy, vz = velocity

		local rocket_left = createProjectile( vehicle, 19, vehicle.matrix.position + vehicle.matrix.right * - 3, rotation, velocity )
		local rocket_right = createProjectile( vehicle, 19, vehicle.matrix.position + vehicle.matrix.right * 3, rotation, velocity )
	end
end
addCommandHandler( "shoot", vMissile )


 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...