General Question about Using Touched on a part or Rays

For a Force Push i would want to make a big radius and now i know with Rays you cant really widen them too much so wouldnt i have to resort back to .Touched on a BasePart?

2 Likes

What do you mean by “force push” and “a big radius”? If you want to get all characters in a sphere (i.e. within a radius of a point), there are better ways of doing so.

He means that it pushes everyone within a radius of the user that initiated this “push” and flings them or so.

yes thats what i did mean, With Rays it seems to not have a radius and its just a (0.5,0.5, Length) and that seems to hold it back.

This sounds like the perfect opportunity for the illusive and scary dot product formula

Basically, you can check the angle between two lines using a “projection” of one line onto the other, getting the length of that “projected” line like so:

This behavior is useful for finding the angle between two vectors because of a couple facts

  1. It creates a right triangle naturally, because it makes a line perpendicular to the line
  2. It gives you a length

Both of these things scream trigonometry

So we need to find the angle between two vectors using a right angle and a length
We would need one more length to find the angle using the formula:

cos(θ) = adjacent/hypotenuse

So we have the side length adjacent to the angle between the two vectors


The general “strategy” for this problem is to make the red line (hypotenuse) have a known length of 1, so its a “unit vector”

We can then use this info to solve using cosine

So you can check if two points are within a “cone” of eachother by checking if the angle is within a certain amount

Example of the code for something like this:

if math.acos((defending_player.Position-attacking_player.Position).Unit:Dot(attacking_player.CFrame.LookVector)) < math.rad(20) then --check if theyre within 20 degrees from where the attacking player is facing
    if (defending_player.Position-attacking_player.Position).Magnitude < 5 then --check if theyre within 5 studs
    --hit registered 
    end
end

Depending on what you use I think you might have to use -LookVector (in the case of hum root part I think) instead of just LookVector but youll just have to see

I saw something close to this, would this youtube video demonstrate that?

Yeah pretty much
You can run into some problems though with players too close with my solution
Let me explain with a small model:

image
Theres no problem at farther distances
But closer up


It could potentially miss

This can be mostly stopped by casting the cone lower, or farther back

The easiest solution is probably to just do that
You will have to edit the code a bit though:

if (defending_player.Position-attacking_player.Position).Magnitude < 5 and (defending_player.Position-attacking_player.Position).Magnitude > 2 then --check if theyre within 2 studs and 5 studs
--the 2 studs is arbitrary, itd be however far back the player is
--a more robust way to do this would also be to do, SEPARATE FROM ALL OTHER IF STATEMENTS:
if (defending_player.Position-attacking_player.Position).Unit:Dot(attacking_player.CFrame.LookVector) < 0 then
--this makes it so you dont have to do the >2 check and is way more accurate

Additionally, in order to move back the cone, instead of doing attacking_player.Position youd do something like

attacking_player.CFrame*CFrame.new(0,0,-3) --either positive or negative cant remember
--the 3 is arbitrary make it as far back as you want

Also I need to bring attention to a mistake which I already will have fixed in my above post
I forgot the math.acos
Apologies

Also for the force choke you probably know you can just use the magnitude checker part, but just in case you didnt know for the future

What i dont get with this is how would you originally reference the Other Player?

I would typically for loop through every player and check each character
This sounds inefficient but really isnt that bad, especially when youre only doing it a few times