Need help casting ray facing the front of a part, but with an offset

I want my ray to shoot in the same direction that my npc is facing, but with an offset down and to the left.

I figured out how to make the offset down, but cant get it to stay left, because I cant get the offset to stay relative to the look direction.

This is where Im at right now

But I want to be here

Heres what my current script looks like:

local Monster = script.Parent
local Core = Monster.Core
local TargetPoint = Monster.TargetPoint
local Target = Monster.Target

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Monster}

while wait() do
	local LookVector = Core.CFrame.LookVector + Vector3.new(0,-0.2,0)
	
	local RayOrigin = Core.Position
	local RayDirection = LookVector * 100

	local RaycastResult = workspace:Raycast(RayOrigin, RayDirection, raycastParams)
	if RaycastResult then
		TargetPoint.Position = RaycastResult.Position + Vector3.new(0,0.5,0)
		if (Target.Position - TargetPoint.Position).Magnitude >= 14 then
			Target.Position = TargetPoint.Position
		end
	end
end

Here’s what my explorer looks like:

image

Ive changed local LookVector = Core.CFrame.LookVector + Vector3.new(0,-0.2,0) to local LookVector = Core.CFrame.LookVector + Vector3.new(0,-0.2,-0.2), and it works like the picture I provide, but once the monster turns, the offset isnt the right way since I dont think its relative. Can someone help me so that it looks like the 2nd picture no matter which way the monster is facing? Thank you in advance!

1 Like

The LookVector is a directional vector of the forward face in global space, I mean it would be pretty useless to have a property of an object that was just always (0, 0, -1).

If we set the LookVector as the origin (0,0,0) then (0,-.2,-.2) is where we’re we will be aiming for. Now it’s as simple as coverting that vector to the world space so it is no longer relative to the LookVector and is the direction we want it to go in the world.

Which looks like this:

LookVector = Vector3.new(0,-0.2,-0.2):VectorToWorldSpace(Core.CFrame.LookVector)
2 Likes

I got this error

VectorToWorldSpace is not a valid member of Vector3

I dont mean to just return an error and make you do the work, I tried looking at the documentation, VectorToWorldSpace is for CFrame but Im kind of lost and don’t know how to fix this. I kind of just guessed and put CFrame instead of Vector3 but nothing happens. Is there any other explanation or help you can provide?

1 Like

The reason that it doesn’t work is because Vector3 doesn’t have a function called VectorToWorldSpace, CFrame does, so instead you need to do

Core.CFrame:VectorToWorldSpace(Vector3.new(0,-0.2,-0.2))

Oh oops! That’s my bad, you said you tried changing Vector3.new() to CFrame.new() right?

I’ll test out the math later when I’m free and get it pinned down, vector math is hard sometimes!

Results in this:

Yep, simply replaced, not sure if Im supposed to alter anything else now that Im on CFrame

local LookVector = CFrame.new(0,-0.2,-0.2):VectorToWorldSpace(Core.CFrame.LookVector)

I hate to do this but I’m going to bump since I still need help. Sorry and thank you in advance.

As I understand you want the ray to shoot in the direction of the npc but with an offset down and to the left

To do that you can define the X, Y and Z components of the vector and then make them relative to the npc’s rotation

First you need to define the X component which is how much the vector is offset in the right direction,
now you need the offset to be the opposite of right which is left so you add a - sign

Then you need to define the Y component which is how much the vector is offset in the up direction,
you need the offset to be the opposite of up which is down so again you add a - sign

And finally, you need to define the Z component which is how much the vector is offset in the backward direction, you can make the ray shoot in the direction of the npc by adding a front offset to the vector
you need the offset to be the opposite of back which is front so yet again you add a - sign

And then you just need to make the vector relative to the npc’s rotation so you can use the VectorToWorldSpace function of CFrame which makes the vector relative to the rotation of the CFrame

The final code should look like this

local LookVector = Core.CFrame:VectorToWorldSpace(Vector3.new(-0.2, -0.2, -1))
1 Like

Literally works perfectly, thank you.

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