Help Creating Custom Axis

Ive been working on a game recently and iv’e run into an issue where i need to define a “custom axis”. I need this to check where the character is on that axis and i’m not exactly sure how to do this.

You could just shoot a raycast between the two parts, and if it sees the player then just compare the players position to the end position to get how far along the line they would be.

Here’s an example I whipped up for that:

local RunService = game:GetService("RunService")

local Part1 = Instance.new("Part", workspace)
local Part2 = Instance.new("Part", workspace)

Part1.Position = Vector3.new(10,3,10)
Part2.Position = Vector3.new(20,3,20)
Part1.Size = Vector3.new(1,1,1)
Part2.Size = Vector3.new(1,1,1)
Part1.Anchored = true
Part2.Anchored = true

local StartPos = Part1.Position
local EndPos = Part2.Position
local Direction = (EndPos-StartPos)
local Param = RaycastParams.new()
Param.FilterDescendantsInstances = {Part1, Part2}
Param.FilterType = Enum.RaycastFilterType.Blacklist

RunService.Heartbeat:Connect(function ()
	local Raycast = workspace:Raycast(StartPos, Direction, Param)
	
	if Raycast ~= nil then
		local Humanoid = Raycast.Instance.Parent:FindFirstChild("Humanoid")
			if Humanoid then
				local Character = Humanoid.Parent
				local HumanoidRootPart = Character.HumanoidRootPart
			local PositionComparedToStart = HumanoidRootPart.Position - StartPos
			local PositionComparedToEnd = HumanoidRootPart.Position - EndPos
			
			print("Compared to StartPos (X, Y, Z): " .. PositionComparedToStart.X ..",".. PositionComparedToStart.Y ..","..PositionComparedToStart.Z .." | ".. " Compared to EndPos (X, Y, Z): ".. PositionComparedToEnd.X ..",".. PositionComparedToEnd.Y ..","..PositionComparedToEnd.Z)
		end
	end
end)

Edit: accidentally printed the same thing twice

1 Like

Ok thanks! Ill try this out and see if it works!

If you are just trying to get the line effect, you can try this:

local part1 = workspace.Part1
local part2 = workspace.Part2

local linePart = workspace.line

local sub = part1.Position - part2.Position

local CF = CFrame.lookAt(part1.Position, part2.Position)

line.CFrame = cf * CFrame.new(0, 0, -sub.Magnitude/2)
line.Size = Vector3.new(.5, .5, sub.Magnitude)

raycasting is necessary as well though if you want to detect the player.

No i need to check the players distance on the “custom axis” but thanks anyways!

Oh. In that case, raycasting is not the most efficient solution.

local part1 = workspace.Part1
local part2 = workspace.Part2

local linePart = workspace.line

local sub = part1.Position - part2.Position

local CF = CFrame.lookAt(part1.Position, part2.Position)

line.CFrame = cf * CFrame.new(0, 0, -sub.Magnitude/2)
line.Size = Vector3.new(.5, .5, sub.Magnitude)

--put in loop
local character = lkfskjljkfjkljklafsdkls --replace with character path
local charCF = character:GetPivot()
local dist = (charCF.Position-CF.Position):Dot(CF.ZVector)
--note: dist might be negative. if that is the case, change 'ZVector' to 'LookVector'

thanks! i think this will work for what i’m creating!

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