mylowoof
|
  |
| Joined: 27 Mar 2014 |
| Total Posts: 167 |
|
|
| 22 Nov 2016 07:17 AM |
I have the LookVector of Part1, and the Position of Part2.
How would i detect whether Part2 is within 45 degrees of Part1's LookVector on the X axis?
Thanks for any help, I kinda suck at CFrame related stuff like this. |
|
|
| Report Abuse |
|
|
mityguy
|
  |
| Joined: 07 Jun 2008 |
| Total Posts: 5483 |
|
|
| 22 Nov 2016 07:25 AM |
| I wish I could help but I've faced a similar issue before. Bump cause I wanna know too. |
|
|
| Report Abuse |
|
|
|
| 22 Nov 2016 07:29 AM |
| Same; I would like to know. |
|
|
| Report Abuse |
|
|
Cheater
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 5258 |
|
|
| 22 Nov 2016 07:44 AM |
I assume you want some kind of detection system. The mathematical word you're looking for is Scalarproduct. You might want to look that up and learn it. It might be difficult at first, just ask some maths teacher. You might even have it at school once (at least we do). In addtion you'll also need the unitvector for this. I think I'll just drop in part of my script where I used it. (I'm honestly proud of this line of code actually :3)
NOTE: This script is inside a "Head" part of an NPC named "Guard". The Guard is inside a model folder named "Guards". The detection is I think 150° (in radians / that's the usual human sight) and it's like a cone, so including the Y and Z axis as well.
Additionally the view ignores certain parts that are inbetween. (Everything that's in the folder "Walls" in workspace) And of course it's only looking to check after players. :) The Scalarproduct is the line where I assign something to the variable "angle" in case you're wondering. Hope it helps!
while wait() do for _, player in pairs(game.Players:GetPlayers()) do distance = player:DistanceFromCharacter(script.Parent.Position) vector_to_player = player.Character.Head.Position - script.Parent.Position angle = (script.Parent.CFrame.lookVector.X * vector_to_player.X + script.Parent.CFrame.lookVector.Y * vector_to_player.Y + script.Parent.CFrame.lookVector.Z * vector_to_player.Z) / (script.Parent.CFrame.lookVector.Magnitude * vector_to_player.Magnitude) print(player.Name.. "'s Distance: " ..distance.. " | Angle: " ..angle) if distance <= 10 and angle >= math.cos(5*math.pi/12) then print(player.Name.. " is getting detected.") ray = Ray.new(script.Parent.Position, (player.Character.Head.Position - script.Parent.Position).unit * distance) for _, wall in pairs(game.Workspace.Walls:GetChildren()) do object, position = game.Workspace:FindPartOnRay(ray, game.Workspace.Guards) if object ~= nil then if object.Parent:FindFirstChild("Humanoid") == nil then print(player.Name.. " is covered.") else print(player.Name.. " is not covered.") end else print(player.Name.. " is not covered.") end end end end end |
|
|
| Report Abuse |
|
|
mylowoof
|
  |
| Joined: 27 Mar 2014 |
| Total Posts: 167 |
|
|
| 22 Nov 2016 08:34 AM |
After cannibalizing the above script, I came up with this:
function Melee(Block, Range, Radians) for _, player in pairs(workspace.Model:GetChildren()) do --local Torso=player.Character.Torso local Torso = player local magnitude = (Block.Position - Torso.Position).magnitude local vector_to_player = Torso.Position - Block.Position if magnitude<=Range then local angle = (Block.CFrame.lookVector.X * vector_to_player.X + Block.CFrame.lookVector.Y * vector_to_player.Y + Block.CFrame.lookVector.Z * vector_to_player.Z) / (Block.CFrame.lookVector.Magnitude * vector_to_player.Magnitude) print(player.Name.. "'s Distance: " ..magnitude.. " | Degrees: " ..math.deg(angle).." Rad:"..angle)
if angle >= math.cos(5*math.pi/12) then print(player.Name.. " is getting detected.") end end end end
It works with no errors, and the "player.Name.. " is getting detected."" output seems correct, but I'm not sure how to work with the Angle output of the script. I converted one of the printed values to Degrees using math.deg. With Part1 looking directly at Part2, it produces an output of:
Part's Distance: 4.3075160980225 | Degrees: 56.605018519481 Rad:0.9879439463173
But look a little to the left, and the output is:
Part's Distance: 4.3038477897644 | Degrees: -27.257123971606 Rad:-0.47572655792879
Something seems to be off here, it doesn't seem like the values should be negative. And I dont see a way to change the field of view cone.
Thanks for the help so far, anyway. |
|
|
| Report Abuse |
|
|
mylowoof
|
  |
| Joined: 27 Mar 2014 |
| Total Posts: 167 |
|
| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 22 Nov 2016 12:39 PM |
| Because that's not the angle, it's the cosine of it |
|
|
| Report Abuse |
|
|
Cheater
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 5258 |
|
|
| 23 Nov 2016 06:14 AM |
| That's true. The variable "angle" is actually representing the cosine and not the actual angle. As you can see at the line below (the if statement), I'm comparing the angle to be equal a certain angle that is converted to a cosine. So basically just take the arccos out of it and you should be fine. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Nov 2016 07:30 AM |
| I was talking about when he printed it. |
|
|
| Report Abuse |
|
|
| |
|