I require some assistance, I am trying to create a raycast with a lookVector however I need to also add an angle to that.
I want to make 2 raycasts.
I am not sure how to achieve these things…
This is what I want to do:
I want to find out how to make these raycasts go in such an angle.
local function Compute()
local rayPositionA = character.HumanoidRootPart --First raycast
local rayDirectionA = character.HumanoidRootPart.CFrame.lookVector * 100 + Vector3.new(0,math.deg(45),math.deg(45))
local rayPositionB = character.HumanoidRootPart --Second raycast
local rayDirectionB = character.HumanoidRootPart.CFrame.lookVector * 100 + Vector3.new(0,math.deg(-45),math.deg(45))
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {character, Baseplate}
local rayA = workspace:Raycast(rayPositionA.Position, rayDirectionA, params) --Casting
local rayB = workspace:Raycast(rayPositionB.Position, rayDirectionB, params)
if rayA then
local part = Instance.new("Part")
part.Parent = rayA.Instance
part.Position = rayA.Position
part.Anchored = true
part.CanCollide = false
part.BrickColor = BrickColor.new("Really red")
end
if rayB then
local part = Instance.new("Part")
part.Parent = rayB.Instance
part.Position = rayB.Position
part.Anchored = true
part.CanCollide = false
part.BrickColor = BrickColor.new("Really red")
end
end
This is what im currently getting:
Any help would be appreciated, Im not that good in the field of raycasting so excuse me if I made any silly mistakes here in raycasting. Tried my best after some research.
I think it might be easier to reason about the problem if you factor out the function to do 1 ray at a time, since the only difference between the two is the inside angle. Here’s the changes I’ve applied:
local function Compute(params, angle)
local rayCFrame = character.HumanoidRootPart.CFrame
local rayDirection = rayCFrame * CFrame.Angles(0, math.rad(angle), math.rad(45))
local ray = workspace:Raycast(rayCFrame.Position, rayDirection.LookVector * 100, params)
if ray then
local part = Instance.new("Part")
part.Parent = ray.Instance
part.Position = ray.Position
part.Anchored = true
part.CanCollide = false
part.BrickColor = BrickColor.new("Really red")
end
end
-- ex. Compute(params, 45), Compute(params, -45)
-- since it's adviced `RaycastParams` only be created once
First, math.deg converts to degrees, so you’d want to use math.rad to convert to radians.
Second, Vector3 + Vector3 just offsets the two vectors by adding them as-is. What you want in this case is to take advantage of CFrames, which have a property of rotation. CFrame * CFrame rotates the first value by the second assuming that the second only has a rotation to it (ex. your_cframe * CFrame.Angles(xrad, yrad, zrad)).
I did not test the example, so it may require some more tweaking, but it should be closer to the desired result.
Thank you for explaining to me more in detail about Vector3 and CFrame and how adding and multiplying works, I was always confused about it and roblox API was confusing to me as well.
Also, the code works pretty well however the parts are shown on the same level as the player, but I want them a little more upwards like in the picture I sent, I changed the X to math.rad(45) instead and that have fixed the issue.
Thank you for your help !