Changing Attachment Orientation Based on Ray

I’m trying to make a push mechanic based on Rays from the Player mouse and VectorForce. The issue I continuously run into is setting the attachment Orientation for the Vector force. Here is what I have:

local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local rayStart = player.Character.HumanoidRootPart.Position
	local raycastResult = workspace:Raycast(rayStart, (mousePosition - rayStart)*100, raycastParams)
	
	if raycastResult then
		local hitPart = raycastResult.Instance
		local model = hitPart:FindFirstAncestorOfClass("Model")
		
		if model then
			if model:FindFirstChild("Humanoid") then
				local attachment = Instance.new("Attachment")
				attachment.Parent = hitPart
				attachment.WorldPosition = raycastResult.Position
				attachment.Orientation = raycastResult.Normal
				
				local forceVelocity = Instance.new("VectorForce")
				forceVelocity.Enabled = false
				forceVelocity.Force = Vector3.new(10500,4000,0)
				forceVelocity.Parent = hitPart
				forceVelocity.Attachment0 = attachment
				forceVelocity.Enabled = true
				wait(0.35)
				forceVelocity.Enabled = false
				
				attachment:Destroy()
				forceVelocity:Destroy()
			end
		end
	end

I’ve tried setting the Orientation to the direction, Normal value, World position…etc. I just can’t figure it out. Thanks in advance!

attachment.Orientation = raycastResult.Normal

You’re setting the attachment’s orientation to the raycast’s normal which won’t make the attachment face the direction of the ray. What you could do is this:

attachment.CFrame = CFrame.lookAt(raycastResult.Position, raycastResult.Normal)

Worth noting that the CFrame property of an attachment is a cframe relative to the parent’s cframe, so it’s an offset in the parent’s object space; rather than it being a cframe in the world space (relative to the origin). You would wanna use WorldCFrame instead.


These are properties you wouldn’t wanna use - orientation is a vector that describes the rotation relative to the attachment’s parent’s rotation where each component represents the angle (in degrees) on the respective axis.

The normal property of the raycast result is a vector that is perpendicular to the surface the raycast hit but that isn’t really useful because you don’t want the player being pushed to get knocked back in the direction opposite of where they are facing, but rather the direction the player pushing is facing.

You can try this:

local targetHrp = model:FindFirstChildOfClass("Humanoid")
local direction = (targetHrp.Position - rayStart).Unit

local attachment = Instance.new("Attachment")
local forceVelocity = Instance.new("VectorForce")

attachment.CFrame = CFrame.new()
attachment.Parent = forceVelocity

forceVelocity.Force = direction * FORCE_MULTILIPLER -- replace this with your desired value
forceVelocity.Attachment0 = attachment
forceVelocity.RelativeTo = Enum.ActuatorRelativeTo.World
forceVelocity.Enabled = true
forceVelocity.Parent = targetHrp

-- rest of the stuff, y'know
4 Likes

That works really well, thank you! Only change I had to make for anyone reading is

if model and model:FindFirstChildOfClass("Humanoid") then
		local targetHrp = model:FindFirstChild("HumanoidRootPart") --or raycastResult.Instance
--and the rest of the example