I am trying to calculate how far to tween a player without it colliding/going through walls by using rays. With the old raycast API, I was able to easily get the end position of the ray, but I am unsure how to do the same things for the new raycast API.
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {player.Character, hithumanoid.Parent}
local origin, direction = player.Character:FindFirstChild("HumanoidRootPart"), player.Character:FindFirstChild("HumanoidRootPart").CFrame.LookVector * 5
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
local tween = game:GetService("TweenService"):Create(hithumanoid.Parent:FindFirstChild("HumanoidRootPart"), TweenInfo.new(0.3), {CFrame = CFrame.new(endPos)})
tween:Play()
Make sure that the variable âoriginâ is a Vector3 position (not a BasePart). workspace:Raycast() returns a RaycastResult or nil. When it returns a RaycastResult, that means that the ray hit something in the workspace, and the RaycastResult will have the properties
RaycastResult.Position (the position of the intersection)
RaycastResult.Normal (the Vector3 normal of the surface hit)
RaycastResult.Material (the material of the object hit)
RaycastResult.Instance (the object hit)
If the raycast result happens to be nil, that means the raycast did not hit anything in the workspace and you must calculate the end of the ray yourself. This can be done by adding the direction vector to the origin vector. You do not have to worry about there being something in the way of the origin and the end of the ray, because the raycast already determined that there was nothing in the way between these two points.
The distance between your raycast and its result would be (Origin - Result.P).magnitude (if you need to know how far to Tween). You can also just use Result.P to Tween towards the general position of the result provided that it exists.
I am trying to determine the end point of a ray without a result, so it should be somewhere in front of the character and should not be hitting anything.
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {player.Character, hithumanoid.Parent}
local origin, direction = player.Character:FindFirstChild("HumanoidRootPart").Position, player.Character:FindFirstChild("HumanoidRootPart").CFrame.LookVector * 5
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
local endPos -- a variable without a value for now
if raycastResult then
-- this means that the raycast did intersect an object and returned a RaycastResult
endPos = raycastResult.Position
else
-- this means nothing was in the way of the ray
endPos = origin + direction
end
Everything works pretty well, but it locks the orientation, how would I make it so that the orientation is consistent to wherever the player moves instead of just turning to one direction?
Another thing you could do is, while playing the animation, change the HumanoidRootPartâs CFrame to look toward the direction of the camera, or toward the direction of the movement of the Humanoid. This can be done using a loop and changing every frame using RunService.RenderStepped (assuming this is in a LocalScript or ModuleScript on the client side). This is a different topic than raycasting though.