Below are a bunch of attempts at raycasting between two Vector3s.
The blue spheres represent start points, the yellow points represent end points, and the red lines between them represent the ray.
Every time the ray is created, the start point is always the characters root part position, and the end point is always the players head position.
The problem is, the places where the rays hit are at the orange points.
In a previous attempt, I noticed that no matter what I did, the rays would always fire from the world origin. ( This is also proof that the code I am using to visualize the rays works. )
My code is very simple (Don’t be overwhelmed, most of it is just creating the point parts):
local StartPosition = Root.Position
local EndPosition = Character.Head.Position
local Raycast = Ray.new(StartPosition, EndPosition)
local Part, Hit = workspace:FindPartOnRay(Raycast, Character)
local function createRayPart(ray)
local part = Instance.new("Part")
local cFrame = CFrame.new(Raycast.Origin, Raycast.Direction) * CFrame.new(0, 0, -(StartPosition - EndPosition).magnitude/2)
part.CFrame = cFrame
part.CanCollide = false
part.Anchored = true
part.Size = Vector3.new(0.2, 0.2,(StartPosition - EndPosition).magnitude)
part.BrickColor = BrickColor.Red()
part.Parent = workspace
end
createRayPart()
--Start point created at StartPosition ( blue ).
local EndPoint2 = Instance.new("Part")
EndPoint2.Material = Enum.Material.Neon
EndPoint2.BrickColor = BrickColor.new("Dark blue")
EndPoint2.Shape = Enum.PartType.Ball
EndPoint2.Size = Vector3.new(1,1,1)
EndPoint2.Anchored = true
EndPoint2.CanCollide = false
EndPoint2.CFrame = CFrame.new(StartPosition)
EndPoint2.Parent = workspace
--End point created at StartPosition ( yellow ).
local EndPoint3 = Instance.new("Part")
EndPoint3.Material = Enum.Material.Neon
EndPoint3.BrickColor = BrickColor.new("Grime")
EndPoint3.Shape = Enum.PartType.Ball
EndPoint3.Size = Vector3.new(1,1,1)
EndPoint3.Anchored = true
EndPoint3.CanCollide = false
EndPoint3.CFrame = CFrame.new(EndPosition)
EndPoint3.Parent = workspace
--If there is a hit, create the orange point at hit vector.
if Part and Hit then
local EndPoint = Instance.new("Part")
EndPoint.Material = Enum.Material.Neon
EndPoint.BrickColor = BrickColor.new("CGA brown")
EndPoint.Shape = Enum.PartType.Ball
EndPoint.Size = Vector3.new(1,1,1)
EndPoint.Anchored = true
EndPoint.CanCollide = false
EndPoint.CFrame = CFrame.new(Hit)
EndPoint.Parent = workspace
end
I have absolutely no idea what is going on. Any help at all would be great.