Raycast is not hitting any parts in its way

  1. What do you want to achieve?
    I’m just doing some learning with raycasting for a project I plan on taking on and I’m just trying to learn how to detect hits and raycasting in general. What I’m trying to accomplish here is just simply printing info about the part the ray is hitting.

  2. What is the issue?
    When I run the game my if statement returns that nothing was hit which is odd since the brick is in the path of the ray.

  3. What solutions have you tried so far?
    I’m unfamiliar with raycasting so nothing yet other than doing some Google searching which turned up with nothing related to my

local rayOrigin = Vector3.new(-17.821, 5, 2.177)
local rayTarget = game.Workspace.hitPart.Position

local rayCastResult = workspace:Raycast(rayOrigin, rayTarget)

if rayCastResult then
	print("Ray Hit!")
	print(rayCastResult.Instance)
	
else 
	print("No target found!")
end

The second parameter is not a target. You need to specify the direction through means of Unit multiplied by length or Magnitude. Here is how:

local rayOrigin = Vector3.new(-17.821, 5, 2.177)
local rayDirection = (workspace.hitPart.Position - rayOrigin).Unit 
local rayLength = (workspace.hitPart.Position - rayOrigin).Magnitude

local rayCastResult = workspace:Raycast(rayOrigin, rayDirection * rayLength)

if rayCastResult then
	print("Ray Hit!")
	print(rayCastResult.Instance)
else 
	print("No target found!")
end

Ohhhh! Thank you, I didn’t read the parameters, was just going off the documentation. Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.