How to get end position of a raycast

So I’m trying to make a move where the player teleports infront of them really quickly, but I want to know how to get the end of a raycast? I’ve tried to use the lookvector of the humanoid root part yet it just spawns them in the middle of the map.

External Media
local Length = 10
			
			local RayParams = RaycastParams.new()
			RayParams.FilterDescendantsInstances = Player.Character:GetDescendants()
			RayParams.FilterType = Enum.RaycastFilterType.Blacklist
			local rayOrigin = Player.Character.HumanoidRootPart.Position
			local rayDirection = Player.Character.HumanoidRootPart.CFrame.LookVector * Length

			local raycastResult = workspace:Raycast(rayOrigin, rayDirection, RayParams)
	if raycastResult then
				Char:MoveTo(raycastResult.Position)
				
	end

The lookvector is a vector3 value RELATIVE to the cframe that your getting the lookvector of. Simply add the lookvector to the cframe to get the new cframe that is in front of the player

HumanoidRootPart.CFrame = HumanoidRootPart.CFrame + HumanoidRootPart.CFrame.LookVector * Length

Thanks so much! another thing is that when the raycast does get a hold of something it teleports me on top of the part, why’s that?

Thats how a raycast works, it returns data about where the raycast hits and nil if it doesnt hit anything. When you hit a part, it returns the position of where it hits the part(including some other data), which is why you teleport to the part. Its sort of like a laser. If you put an object in front of the laser, the laser wont pass through the object and will just stop wherever it made contact with the object. That contact spot is basically what a raycast returns

okay, so how do I get the direct end position of the raycast?

If you mean the spot where the raycast made contact, your doing it correctly. However with what your trying to acheive, I dont think you should be using raycasts and instead just add the lookvector to the HumanoidRootPart’s CFrame

I don’t want it to go past parts thats the thing, I want it to be like if theres a part it just teleports the player at the position infront of them instead of directy placing them past.

Oh. In that case you would have to check if your raycasts hits anything. If it does, you would use your normal raycast return value. If it doesnt, you can just add the raycast direction to the origin.

local Length = 10
			
			local RayParams = RaycastParams.new()
			RayParams.FilterDescendantsInstances = Player.Character:GetDescendants()
			RayParams.FilterType = Enum.RaycastFilterType.Blacklist
			local rayOrigin = Player.Character.HumanoidRootPart.Position
			local rayDirection = Player.Character.HumanoidRootPart.CFrame.LookVector * Length

			local raycastResult = workspace:Raycast(rayOrigin, rayDirection, RayParams)
	if raycastResult then
				Char:MoveTo(raycastResult.Position)
				
	else
                  Char:MoveTo(rayOirign + rayDirection)
        end

Should I check if the raycast hit something? then find the distance between the player and the raycast and place them on the distance ?

the first part of the if statement handles that already. It checks if you hit something and if you did, it places it wherever the raycasts hits. The second part of the if statement runs if nothing is hit, which just places the player at the end of the raycasts

The thing is if it does hit something, it places me on top of the part it hit, how would I avoid that?

Not sure if this is right but I think MoveTo() moves a model upwards until it no longer collides with its surroundings. I would just directly change the CFrame instead

local Length = 10
			
			local RayParams = RaycastParams.new()
			RayParams.FilterDescendantsInstances = Player.Character:GetDescendants()
			RayParams.FilterType = Enum.RaycastFilterType.Blacklist
			local rayOrigin = Player.Character.HumanoidRootPart.Position
			local rayDirection = Player.Character.HumanoidRootPart.CFrame.LookVector * Length

			local raycastResult = workspace:Raycast(rayOrigin, rayDirection, RayParams)
	if raycastResult then
				 Char.HumanoidRootPart.CFrame = CFrame.new(raycastResult.Position)
				
	else
                  Char.HumanoidRootPart.CFrame = CFrame.new(rayOirign + rayDirection)
        end
1 Like

dude thanks so much !! I didn’t know there was a difference between them!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.