Find an angle between 2 vector 3's and then shoot a raycast using that angle

I want to make a raycast but i dont want the raycast to be shot from an angle but to be shot from a position in the game like

local raycastResult = workspace:Raycast(HRP, A Position in the workspace)

but I get an error called
Unable to cast Instance to Vector3
because u cant cast a raycast to an instance in space

1 Like

did you forget to add .Position?

:Raycast(HRP.Position, A Position in the workspace)

nope, Here is the code im trying

local raycastResult = workspace:Raycast(HRP, SnapHRP)

HRP is the humanoidRootPart
And SnapHRP is the Position i want to check

Also, that’s not really how Ray’s work

Rays Require a Starting Position, which is a Vector3
Rays also need a Direction for the ray to go to, which is a Vector3

So yeah, Use HRP.Position

well i need a way to check is a space is used in space not using a direction but a position

you can Filter Descendants using RaycastParams so it doesn’t hit anything while checking

So in your code you would do:

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {Array of Items}

local raycastResult = workspace:Raycast(HRP, SnapHRP, Params)

You need Position and Direction for a raycast, i’m also not understanding your issue and what you mean with “if space is used in space”

1 Like

Im making a building system and i need to check if the block Infront of the player is used if it is used to build 1 block infront

Then you raycast from the cameras position and its lookvector, and you only whitelist building blocks so you wont detect anything else

Then you can use CollectionService or just Properties or whatever you want to check if the block is “used”

its a building system where u can build ahead, not a building system using mouse position so i need to check if there is a block infront of it so i can place a block +1 infront of it so i cant use mouse just a position in space

So you want to cast it from Vector A to Vector B. Well to be quite frank you can do this by using:

local Part1, Part2 = ...
local direction = Part1.Position - Part2.Position

This shows the directional vector from Part1 to Part2.
Instead if you prefer a CFrame use

CFrame.new(Part1.Position, Part2.Position)

This creates a CFrame which is positioned on Part1, but oriented towards Part2.

the point is that i dont have the position of part 2 i need to check if there is a part infront of it

image

Then do a raycast for each surface of the block to check what blocks are around it

is there a way i could find an angle between the 2 vector3’s and use that angle to shoot a raycast

I don’t understand what you need the angle for. Can’t you just cast a ray in the downwards direction, from the X and Z positions of the block but with the Y axis of the root part?

For example,

for each block do
    workspace:Raycast(Vector3.new(block.X, root.Y, block.Z), -CFrame.identity.UpVector * length)

i am trying to make a system where u can build ahead of your self so i need to check if there is a block already there so it can build 1 block ahead of that part

I think I understand, in that case couldn’t you just cast a ray every x studs defining an origin using the root’s look vector? It would likely be helpful if we had shaped raycasting (especially linecasting) but we don’t so the next best alternative would likely be to use a ray every x studs.

So for example:

for i = 1, numberOfRays do
    local position = root.Position + root.LookVector * i * increment
    local rayResult = workspace:Raycast(position, -CFrame.identity.LookVector * rayLength
    if not rayResult then
        there is no block here, place the block
        break
    end
end

what do you mean by root???

Root as in the humanoid root part

1 Like