Spawn a part based off LookVector on the Z axis

I want to spawn a part based off the yellow lines X and Y position, which works.
The only thing I need to change is the Z position, which has to be based off the LookVector the character is facing at the time I hit the ball. I tried firing a raycast, which I visualized as the red line, and then took its position.Z. However, this doesn’t work, as the green part spawns way off to the sides, not even close to the raycast.

https://gyazo.com/ae59673e340bd07546cbcbb82108e88f

How would I make the part spawn exactly on the same Z axis as represented by the red line? The part has to be positioned exactly straight ahead of the player - basically based off the lookvector of the humanoidrootpart.

local DebrisService = game:GetService('Debris')
function SpawnTestPart(HumanoidRootPart, Endpoint)
	local ray = Ray.new(HumanoidRootPart.Position, HumanoidRootPart.CFrame.LookVector * 100)
	local hit, position, normal = workspace:FindPartOnRayWithIgnoreList(ray, {HumanoidRootPart.Parent, workspace.Courts, workspace.EffectsCollection, workspace.BallFolder});
	
	if position then
		print(position.Z)
		local distance = (HumanoidRootPart.Position - position).Magnitude
		local p = Instance.new("Part")
		p.Anchored = true
		p.CanCollide = false
		p.Size = Vector3.new(0.1, 0.1, distance)
		p.CFrame = CFrame.new(HumanoidRootPart.Position, position)*CFrame.new(0, 0, -distance/2)
		p.Parent = workspace
		p.Material = Enum.Material.Neon
		p.BrickColor = BrickColor.new("Really red")
		DebrisService:AddItem(p, 2)
	end
	
	local pos = Vector3.new(Endpoint.Position.X, Endpoint.Position.Y, position.Z) -- This is where I use the position returned by the raycast, to get a Z position based off the Lookvector of the HumanoidRootPart.
	local Part = Instance.new("Part")
	Part.Anchored = true
	Part.CanCollide = false
	Part.CFrame = CFrame.new(pos) -- This is where I'm setting the position of the green part. This is 
	Part.Material = Enum.Material.Neon
	Part.BrickColor = BrickColor.new("Bright green")
	Part.Parent = workspace
	DebrisService:AddItem(Part, 2)
	return pos
end

The green part has to spawn at the blue circle:

I have a question.
Why are the red and green parts spawned?
What are their purposes?

The red part is to visualize the raycast I’m sending out. The intention behind the raycast was to use its returned position and then take the Z position from it. As shown in my GIF, this doesn’t work unfortunately.

The green part is what I’m trying to spawn exactly infront of the player, based on the lookvector. Another requirement is it has to spawn on the yellow line’s X and Y position, but this part works. My issue is trying to get a Z position based off the LookVector of the player. The Z position has to be somewhere that’s facing exactly straight ahead of the player

I think the issue is the raycast isn’t colliding with anything before it reaches the end of it’s distance. (From my assumption the Z axis is parallel with the net)

Visual of the issue I believe is occurring

I would recommend using WorldRoot:Raycast and create a new CollisionGroup and assign that to the yellow part (And increase the height of the yellow part). So that when you raycast it will collide with the yellow wall and not continue the raycast to the full distance.

So your idea behind doing all that is to make the raycast hit the yellow part and stop at the yellow part’s position?

And yes, the Z axis is parallel to the net :smile:

Yeah, I just created an image of what I think is happening, which is based off the raycast not colliding with anything so the hit position of the raycast is outside the court boundaries.

Yeah I can see your logic behind that. A question though, if I manually just make the raycast only go out to the line. Let’s say this would maybe be:

local ray = Ray.new(HumanoidRootPart.Position, HumanoidRootPart.CFrame.LookVector * 34)

Wouldn’t that produce the same results? Or do you think that the ray needs to hit something, in order to fix this issue?

I’m wondering because this also happens:

https://gyazo.com/159b1cc59f430f163c5d32285ec484ed

I mean if you want, what you could do is create one raycast that can only collide with the back wall. This would be used to get the length until the backwall, and then use a second raycast using that length but this raycast can collide with anything.

But you will need at some point have a boundary wall since using Position values from base parts won’t work to get an accurate distance.

1 Like

Alright thank you. I’ll try what you said :smiley: