How would I raycast at an angle?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m working on a board game game and I’m trying to cast a ray from a piece in a 45 degree ray.
  2. What is the issue? Include screenshots / videos if possible!
    How would I go on about casting it in a 45 degree ray, as you can only use Vector3?
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried using CFrame and quickly realized I can’t.

You can use YourCFrame.lookVector, which gives you a direction for the ray.

1 Like

How would I do that? when i do:

local dir = piece.CFrame.LookVector

It returns nil every time, even though I rotate the model.

*by model I mean part. That was a typo.

Can you show the code where you have it implemented?

I just have it print the instance for testing purposes.

I’m not sure what type of game you’re making, but you can cast a Ray 45 degrees downward from the front of a piece using what @JarodOfOrbiter said:

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {model}
raycastParams.FilterType = Enum.RaycastFilterType.BlackList
local rootPart = model.PrimaryPart
local origin = rootPart.Position
local distance = 8
local direction = (rootPart.CFrame.LookVector - rootPart.CFrame.UpVector).Unit * distance
local raycastResult = workspace:GetPartOnRay(origin, direction, raycastParams)

if raycastResult then
-- Code
end

Something like this.

2 Likes

I think he means 45 degrees along the board like how a chess piece would move, in which case youd do exactly this but replace upvector with rightvector

You can also get different 45 degree angles by doing:
-lookvector-rightvector (this gets back left)
Or
-lookvector+rightvector (this gets back right)
Or
lookvector-rightvector (this gets front left)
or
lookvector+rightvector (this gets front right)

But yeah just do what he said but make those small edits

3 Likes