How to get a point x studs in a direction?

I’m going to post pictures for a lack of a better explanation and then post what I have tried.
image

local function GetStrafePosition()
	local Direction = (HRP.Position - NPCHRP.Position)
	local StrafePosition = HRP.Position + Direction * StrafeDistance
	return StrafePosition
end

I tried to pass this function in my pathfinding and it’s returning some position god knows where, in some unobtainable position on the other side of the map.

I’m just stumped. Any help would be appreciated. Thanks.

2 Likes

Have you tried to use raycasting from your origin to your direction positions to get the position for your pathfinding?

Yes, I have. However, I found an issue with getting the raycastresult position when there’s no hit

What’s the script you use for your raycasting?

local function GetStrafePosition()
	--[[local Direction = (HRP.Position - NPCHRP.Position)
	local StrafePosition = HRP.Position + Direction * StrafeDistance
	return StrafePosition]]--
	local StrafeRR = SharedLib:CastRay(HRP.Position, NPCHRP.Position, 15, {Player.Character})
	if StrafeRR then
		print(StrafeRR.Position)
		return StrafeRR.Position
	elseif StrafeRR == nil then
		print('Couldnt cast ray?')
	end
end
function SharedLib:CastRay(StartPoint, EndPoint, MaxDistance, IgnoreList)
	local Params = RaycastParams.new()
	if IgnoreList ~= nil then
		Params.FilterDescendantsInstances = {IgnoreList}
	end
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	
	local Direction = EndPoint - StartPoint
	local RaycastResult = workspace:Raycast(StartPoint, (StartPoint - EndPoint).Unit*-MaxDistance, Params)

	return RaycastResult
end

Prints “Couldnt cast ray?” every time.

Try changing your Direction variable to

local Direction = (EndPoint - StartPoint).Unit -- gives a direction going 1 stud
local RaycastResult = workspace:Raycast(StartPoint, Direction * MaxDistance, Params)

and also multiply your Direction by a non negative number or it goes backwards

This still returns the same result.



image

Perhaps you want to use a magnitude check to make sure that NPCHRP.Position is within 15 studs of your character, or your ray will return nil because its too far. For testing purposes, try making MaxDistance 1000.

It returns a position if I set the maxdistance to 1000, but doesn’t this defeat the point? I’m attempting to get the position 15 studs away from the player, not 1000.

So you want a point 15 studs infront of the character in the direction of the npc?
I’m a bit confused.

That is why it is for testing purposes. We made maxdistance 1000 to see if it gets a position and since it does, change it back to 15. The only option i can think of now is that you print the magnitude between the two positions

The magnitude between the two positions will be greater than 15. I’m attempting to move it to that position, not check if it’s already at that position.

Im not sure if I understand the question very well, but I’ll try to help.

Correct me if I misunderstood.
So basically, you have a startPosition and an endPosition that you would like to point 15 studs in the direction of?

So, something like

--this is pseudo-code cuz I'm on my phone
dir = (endPos - startPos).Unit * 15

?

Direction = (CFrame.lookAt(HumaniodRoot.Position, NPCRoot.Position) * CFrame.new(0, 0, -15)).Position Could work too, but im unsure about the question too.

Check out the others cause they are better lmao

No. The direction that the player is facing is irrelevant. Basically you’re taking the direction TO the NPC from the player’s character, then getting 15 studs along that “line” of direction.

Then I move the NPC to that point.

Thats what I meant, and I just gave an answer :sweat_smile:

1 Like

I’ll try it out now. Thank you!

1 Like

It could work but it is overcomplicated for such a small task, all you need is a position, bringing CFrame into this is unnecessary.

then

local Direction = CFrame.new(StartPoint, Direction).LookVector * MaxDistance 

should give you the direction 15 studs along the line you were talking about

1 Like

Yes, other methods do work, I just completely had a blank mind when making that

1 Like