I was making a raycast for a wall hop system, but I’m having issue where the raycast doesn’t go from the players humanoid root part to a straight direction.
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local root = char.PrimaryPart
local origion = root.CFrame.Position
local direction = root.CFrame.LookVector.Unit * Vector3.new(8, 1, 8)
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.IgnoreWater = true
params.FilterDescendantsInstances = {char}
local part = Instance.new("Part", workspace)
part.Anchored = true
part.Position = origion
part.Size = direction
part.CanCollide = false
part.Name = "Visualizer"
game:GetService("RunService").RenderStepped:Connect(function()
origion = root.Position
direction = root.CFrame.LookVector.Unit * Vector3.new(8, 1, 8)
part.Position = origion
part.Size = direction
local forwardRay = workspace:Raycast(origion, direction, params)
part.Position = origion
part.Size = direction
if forwardRay then
print(forwardRay.Instance)
end
end)
I’m trying to shoot a ray from the humanoid root part (origin) directly forward 2 units ahead of the origin (direction). The issue is that, the ray’s vizualizer (the part in the video) moves in a strange way, and doesn’t seem to reflect the direction of the ray.
I always want the ray to be infront of the humanoid root part, but it doesn’t seem to be going in that direction.
And also, I want the ray to shoot like this, and for the visual part reflecting the ray to look like this.
The part is usualy the size of {0.001, 0.001, 0.001} though.
This is the problem.
You want the position to be something like this:
part.CFrame = root.CFrame * CFrame.new(0,0,-2) -- Either X or Z needs to be offset.
part.Size = Vector3.new(1,1,2) -- either X or Z is the length, this needs to be the length of the raycast or the distance from where it hit.
I dont really suggest this I suggest something like this more instead:
This is wrong, you have to get the direction still, but you dont need the look direction, you want the orientation plus the postion, this is done by getting:
root.CFrame
Now we want to move it forwards so we’d need to add one of the following:
Now we have offset the part correctly, we can calculate the size by the length of the raycast, but then we would need to offset the CFrame more so replace the 2 with rayCastLenght/2
and make the size either: