Here’s the code i use
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Character}
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
local Origin = Player.Character.HumanoidRootPart.CFrame.Position
local Destination = hit
local Direction = (Destination - Origin)
local RayCast = game.Workspace:Raycast(Origin,Direction,RayParams)
local Variable = ?
I want the Variable to be a position that is 50 studs away from the character in the direction of the RayCast.Instance.Position. I’ve tried setting the length of the direction to one stud then multiplying
Direction = (Destination - Origin).Unit * 50
But this only limits the range of the RayCast, and that’s not what i want
I’m unclear what you’re wanting, because this should be the solution you’re looking for: A 50 stud long ray in the direction of origin to destination. Why is this unsatisfactory?
Without doing .Unit * 50
, the length of the ray is whatever the distance between Destination and Origin are.
After reading a little more carefully, perhaps what you want is the position based on the point on the instance that the raycast hit:
local Variable = Origin + (RayCast.Position - Origin).Unit * 50
(Don’t forget to validate that RayCast exists, as the Raycast method may return nil)
2 Likes
Let me clarify on what i want.
(Destination - Origin).Unit*50
Limits the Raycast’s range to 50 studs, which might sound like what i want at first but it isn’t. I want a point between the Origin
and RayCast.Position
I think my latest answer gives you exactly that. Does it work as intended?
1 Like
I’ve tried it and it doesn’t work. Here’s a screenshot:
Green dot: Myoriginal
HumanoidRootPart.position
Red Cube:
Raycast.Position
White dot: The Variable
Code:
local Origin = Player.Character.HumanoidRootPart.CFrame.Position
local Destination = hit.Position
local Direction = (Destination - Origin)
local RayCast = game.Workspace:Raycast(Origin,Direction,RayParams)
if RayCast then
local Variable = (RayCast.Position - Origin).Unit*50
WhiteCube.Position = Variable
end
Variable is a vector relative to Origin but it is being positioned at the world’s origin (0, 0, 0) instead so it isn’t in the correct position. You want to add the relative vector to the Origin to convert it to a vector in the world’s space (relative to the world’s origin):
WhiteCube.Position = Origin + Variable
1 Like
Edited my post: I forgot to add the Origin’s position to the offset vector. Should be correct now!
1 Like