Need help with getting a forward position of a part regardless of it's rotation

Hey, I want to fire a ray from the from of the character’s right arm.
My issue is that using lookVector it will fire a ray in front of the while keeping in mind it’s rotation (gif shows me moving and shooting the ray).

Code:

	local origin = RArm.Position
	local direction = RArm.CFrame.lookVector * 5
	local result = workspace:Raycast(origin,direction)

As you can see while my arm is moving the ray will be rotated with it, I want it to keep attempting to fire the ray forward of the arm, I thought about using ToWorldSpace or PointToWorldSpace and stuff like that, but I can’t understand anything about it…

Thanks in advance its 3:30 AM and I will be heading off and reading tomorrow :wink:

–bump-- character limitation:melting_face:

Simply remove the direction’s vertical component.
Instead of:

local direction = RArm.CFrame.lookVector * 5

write:

local direction = (RArm.CFrame.lookVector * Vector3.new(1, 0, 1)).Unit * 5

* Vector3.new(1, 0, 1) zeroes out the Y axis (whatever it might be).
.Unit normalizes the direction again, otherwise the ray could be shorter than 5 studs.

The problem with this approach is that if the player is jumping and the arms are upside down, the ray will be cast behind the player, not in front of it.

But you seem to be raycasting exactly in line with the sword. If the raycast were to check for collision with the sword, then it wouldn’t make sense to make it not hit in line with the sword. Your current code is working exactly as it should.

4 Likes

Yeah you are right about the last part, I have tested both things and what you say makes sense, i’ll stick to my original code but good to know this anyways!