Way to convert position to direction? (Raycast)

Context:
I am working on a attack move for an ability inside of my game. The ability is called C-Moon and is entirely based off of the anime or manga JoJo’s Bizarre Adventure. The move has a two key press system. The first press is a slam into the ground which will make some chunks of debris ascend from the ground. Upon the second press, the chunks will be shot towards the player’s cursor. Scripting wise, everything is done. The only thing is that the debris does not shoot the direction I want it to, this is what I require help with.

My current system:
The script for the move is inside of a server script. So it fires a remote function to get the player’s Mouse.Hit CFrame. It then creates a part (“MousePosition”) at the position of the mouse. All good so far, but here is where the issue comes up. What I want is for the first chunk of debris to shoot a raycast towards MousePosition, and if anything is in the way, it will create a new part (“FinalPosition”) and shoot the debris towards there.

Issue:
:Raycast(StartingPosition, DIRECTION)
The direction of where the ray needs to be shot isn’t a final position, but a direction. I have no idea how I could convert the MousePosition.Position to the debris’s direction towards the MousePosition.

This is my code:

	local MousePosition = Instance.new("Part")
	MousePosition.Parent = game.Workspace.GameEffects
	MousePosition.CanCollide = false
	MousePosition.Anchored = true
	MousePosition.Transparency = 1
	MousePosition.CFrame = MouseCFrame

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Character, Stand, MousePosition, Character:FindFirstChild("DebrisGravity1"), Character:FindFirstChild("DebrisGravity2"), Character:FindFirstChild("DebrisGravity3")}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local RayShot = game.Workspace:Raycast(Character:FindFirstChild("DebrisGravity1").Position, MousePosition.Position, raycastParams)
        -- Issue is above, MousePosition.Position doesn't work, I need to find a way to make it a direction towards the MousePosition instead of an actual position.

	local FinalPosition = Instance.new("Part")
	FinalPosition.Parent = game.Workspace.GameEffects
	FinalPosition.CanCollide = false
	FinalPosition.Anchored = true
	FinalPosition.Transparency = 1

	if RayShot then
		FinalPosition.Position = RayShot.Position + RayShot.Normal
	else
		FinalPosition.Position = MousePosition.Position
	end

You need a starting point/origin, like the position of the player:

local playerPosition = -- could be the position of the head or humanoid root part
local range = -- will be the max range where the ray cast can reach

local direction = (mousePosition - playerPosition).Unit * range
1 Like

Works perfectly, thank you for the help!

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