Hello. So i'm new to raycasting, and i'm having a problem with it. You see, i have an AI and i want him to jump if there's a part in front of him. And i would like to use raycasting. Though i'm not really sure on how do i make a ray that starts somewhere near the torso and ends somewhere in front of the torso. Kinda of like this photo: http://prnt.sc/8llfmp
Though i wasn't successful. The ray wasn't appearing in the front of the torso.
Here's what i got:
local rayStart = script.Parent.Torso.Position - Vector3.new(0,3,0)
local rayLook = rayStart+script.Parent.Torso.CFrame.lookVector
local ray, pos = Ray.new(rayStart,(rayLook-rayStart).unit)
local partOnRay = game.Workspace:FindPartOnRay(ray,script.Parent)
UPDATE: In my script, i created a part that's welded on the character and positioned on the position i wanted. With that, i could shoot the ray without making some bizarre math. Though, i have testing your answers and for some reason the ray is being casted from the part and it goes to the center of the world. I have checked if the part is in the right position and it is and it's also rotated in the correct rotation. Help?
Entire script (Raycasting line: 66):
-------------
--Variables--
-------------
local TC = script.Parent.Configuration.TeamColor.Value
local humanoid = script.Parent.Humanoid
local Pathfinding = game:GetService("PathfindingService")
local target = script.Parent.Target
local torso = script.Parent.Torso
---------------------
--Instance Creation--
---------------------
local rayPart = Instance.new("Part",script.Parent)
rayPart.Transparency = 1
rayPart.Size = Vector3.new(1,1,1)
rayPart.Position = Vector3.new(torso.Position.X,torso.Position.Y-2,torso.Position.Z+1)
rayPart.CanCollide = false
rayPart.Rotation = torso.Rotation
local weld = Instance.new("Weld")
weld.Part0 = torso
weld.Part1 = rayPart
weld.C0 = CFrame.new()
weld.C1 = rayPart.CFrame:inverse() * torso.CFrame
weld.Parent = torso
-------------
--Functions--
-------------
--Move to Spawn Points
function MoveToSpawnPoint(s)
--Creates variables and path
local PF = Pathfinding:ComputeSmoothPathAsync(script.Parent.Torso.Position,s.Position,512)
local points = PF:GetPointCoordinates()
--visualizePath(points)
--Moves through the path
local distance;
local br = false
for i = 1,#points do
wait(0)
--Breakes to:
--Recreate the path or;
--To move to an enemy if exists
if br or target.Value ~= nil then break end
--Move to the created points
local count = 0
repeat
--Turns br(break) to true if needed
count=count+1
if count>20 or target.Value ~= nil then
br = true
break
end
--Move through the path
if script.Parent == nil or script.Parent:FindFirstChild("Torso") == nil then
return
end
humanoid:MoveTo(points[math.min(#points,i+1)],nil)
local start = rayPart.Position
local finish = (start-rayPart.CFrame.lookVector).unit*15
local ray = Ray.new(start,finish)
local partOnRay = game.Workspace:FindPartOnRay(ray,script.Parent)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Bright red")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (start - finish).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(start, finish) * CFrame.new(0, 0, -distance / 2)
if points[i].Y>script.Parent.Torso.Position.Y+2.5 or partOnRay~=nil then
humanoid.Jump = true
end
distance=(points[i]*Vector3.new(1,0,1)-script.Parent.Torso.Position*Vector3.new(1,0,1)).magnitude
wait()
until distance<5
end
end
--Move to Enemy
function MoveToEnemy()
if target.Value == nil then return end
if (script.Parent.Torso.Position-target.Value.Torso.Position).magnitude >=10 then
--Creates variables and path
local PF = Pathfinding:ComputeRawPathAsync(script.Parent.Torso.Position,target.Value.Torso.Position,50)
local points = PF:GetPointCoordinates()
local distance;
local br = false
--Move through path
for i = 1,#points do
wait(0)
--Breakes to:
--Recreate the path;
--Move to a spawnpoint when the target doesn't exist or;
--Move to the enemy without pathfinding
if br or target.Value == nil then break end
--Move to created points
local count = 0
repeat
--Turns br(break) to true if needed
count=count+1
if target.Value == nil then
br = true
break
end
if count>20 or (script.Parent.Torso.Position-target.Value.Torso.Position).magnitude <10 then
br = true
break
end
--Move through the path
humanoid:MoveTo(points[math.min(#points,i+1)],nil)
if script.Parent == nil or script.Parent:FindFirstChild("Torso") == nil then
return
end
if points[i].Y>script.Parent.Torso.Position.Y+2.5 then
humanoid.Jump = true
end
distance=(points[i]*Vector3.new(1,0,1)-script.Parent.Torso.Position*Vector3.new(1,0,1)).magnitude
wait()
until distance<5
end
end
--Move to the enemy without pathfinding if he's close
if target.Value ~= nil then
if (script.Parent.Torso.Position-target.Value.Torso.Position).magnitude <10 and target.Value.Humanoid.Health>0 then
humanoid:MoveTo(target.Value.Torso.Position)
end
end
end
-------------
--Task Area--
-------------
while wait(0) do
--Move to target if it exists
if script.Parent.Target.Value then
MoveToEnemy()
--If theres not any enemy close, then go to the spawn points
elseif script.Parent.Target.Value == nil and script.Parent.Spawn.Value ~= nil then
MoveToSpawnPoint(script.Parent.Spawn.Value)
end
end

8 comments
That photo is the way i wanted my ray to be like. Though i used the script and the ray starts at the AI and goes to the center of the world
Elaborate?
http://prnt.sc/8llfmp. I want my ray to look like this (Without the part. Thats were i want my ray to start and to finish).
I don't know what that means but if you want it to go to the center then make the direction Vector3.new(0,0,0)
NO. See that gray part. Thats what i want my RAY to START and FINISH.
@Drahsid5 The main problem with this is that you've mistaken the return value of lookVector. Subtracting the torso's position is irrelevant once you have the direction the torso is facing. With this code, you're essentially basing the direction of the ray on its position from the origin, which doesn't really fit the context.
@Legojoker did I? Maybe I'm too used to using m.hit
Try out my new code