Rays always shooting from (0,0,0)

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.

3 Likes

This doesn’t make any sense to me. If you are drawing a ray between the Character’s root part and head, then how could the ray be hitting the object multiple studs away?

Can you please explain what you are attempting to do?

What have you tried? You have listed what is happening currently, but not how you have gotten to this point or what your goal is. Please give us more details if you would like a resolution to this problem, thanks!

Is the start position updated after the first time StartPosition is declared? Because if it’s not the root’s position might’ve been close to or is 0,0,0 when it first started and was saved lime that

It makes no sense to me either. I am plugging in valid Vector3’s to construct my rays, but they are just totally screwed up.

I am trying to cast a ray between two Vector3 positions.

The character is no where near the root on game start. When I print the StartPosition and EndPosition, they update correctly.

local StartPosition = Root.Position		
local EndPosition     = Character.Head.Position
			
local Raycast = Ray.new(StartPosition, EndPosition)

the second argument to Ray.new() is the direction. Not the target vector.

If you want to ray from the root to the head you’d need to do something like this

local startPosition = root.Position
local endPosition = character.Head.Position - startPosition
local ray = Ray.new(startPosition, endPosition)

By subtracting the head position from the startPosition, you get the directional vector including length that you’d need to raycast from start to end.

6 Likes