Forum

> > CS2D > Scripts > Anticipate if you shoot it hits a target or not
Forums overviewCS2D overview Scripts overviewLog in to reply

English Anticipate if you shoot it hits a target or not

2 replies
To the start Previous 1 Next To the start

old Anticipate if you shoot it hits a target or not

The Dark Shadow
User Off Offline

Quote
Recently while i was creating custom weapons script I got stuck in this thing, So basically what I want is anticipate whenever my aim (ex) is at a decent position so if i shoot if it could hit a target print an output.

Here, a simple script that prints "Yes" if player Id 1 (mousemapX) and (mousemapY) are within player Id 2 (target) (X,Y) in 16 pixels each side for both coordinates, elsewise it prints a "No". It doesn't work the best though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
addhook("ms100", "ms100_hook")
function ms100_hook()
	local You = 1
	local Target = 2
	
	local radius = 16

	local mouseMapX = player(You, "x") - player(You, "screenw")/2 + player(You, "mousex")
	local mouseMapY = player(You, "y") - player(You, "screenh")/2 + player(You, "mousey")

	local inRange_X = mouseMapX >= player(Target, "x") - radius and mouseMapX <= player(Target, "x") + radius
	local inRange_Y = mouseMapY >= player(Target, "y") - radius and mouseMapY <= player(Target, "y") + radius
	
	if inRange_X and inRange_Y then
		parse("hudtxt 0 Yes")
	else
		parse("hudtxt 0 No")
	end
end

If my explanation above was poor here an explanation in images:

IMG:https://i.imgur.com/Qc1L0XM.png


So using the code above in the first image it prints "Yes" at this point as If I shoot It will hit the target. √

IMG:https://i.imgur.com/RTJHiaY.png


But in the second image if the code above is being used it prints "No" even though still if I shoot it will hit the target. ×
It should print "Yes".

Note: Only If the target is being seen within the screen.

Does anyone know the "maths" behind this thing? Thanks, Would be appreciated!
edited 6×, last 14.04.21 11:25:16 am

old Re: Anticipate if you shoot it hits a target or not

ohaz
User Off Offline

Quote
What you want to do is create a line (y=mx+t) between your character and the cursor.
First you have to calculate m:
m = (y2-y1) / (x2-x1)
Then you can calculate t by inserting x and y of the player in y=mx+t:

y1 = m * x1 + t

Then you can insert m and t into the formula: y = mx+t (you'll get something like y = 2x + 5
After that you need a circle/line or rectangle/line intersection. Both of those are not super trivial, but there are some stackoverflow posts for it: https://stackoverflow.com/questions/1073336/circle-line-segment-collision-detection-algorithm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview