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.
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 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)
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.
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.
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.