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.
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
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
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
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
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