Creating a part which is 50 studs away from where the torso is facing

I want to make a part which is 50 studs away from where the torso is facing, like this

This is my script I have tried

local uis = game:GetService("UserInputService")
local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer
plr.CharacterAppearanceLoaded:Wait()
local char = plr.Character

uis.InputBegan:Connect(function(input, istyping)
	if input.KeyCode == Enum.KeyCode.F and not istyping then
	
		local attachment0 = Instance.new("Attachment")
		attachment0.Name = "Attachment0"
		attachment0.Parent = plr.Character.Torso
		
		print(plr.Character.Torso.Position)
		
		local End = Instance.new("Part")
		End.Name = "AttackBeamTarget"
		End.Anchored = true
		End.Parent = workspace
		End.Rotation = plr.Character.Torso.Rotation
		End.Position = Vector3.new((plr.Character.Torso.Position.X + 50), plr.Character.Torso.Position.Y, plr.Character.Torso.Position.Z)
		
		
		
		local attachment1 = Instance.new("Attachment")
		attachment1.Name = "Attachment1"
		attachment1.Parent = End
	end
end)

The problem is that it doesnt go where its facing and i dont know how to fix it

1 Like

this does not get where they are looking and instead puts it 50 studs in the positive direction of the x-axis. Instead you should try something like this:
End.Position = plr.Character.Torso.Position + player.Character.Torso.CFrame.LookVector*50

1 Like

You have the right idea, but vectors don’t account for rotational data. There are two ways to circumvent this:

The first is CFrame manipulation, which applies transforms from a relative scope:

part.CFrame = torso.CFrame * CFrame.new(0, 0, -10);

When manipulating CFrames, multiplication will almost always be your dominant operator. Note how instead of applying a transform on the x-axis, I instead put it on the z-axis since it denotes forward and backward placement:
image

The second option is Vector3 manipulation like you did, except we determine the final offset based on the rotation of the torso (simply put):

part.Position = torso.Position + torso.CFrame.LookVector * 10;

Look vectors are generally difficult to understand without prior knowledge in trigonometry, so I won’t dive too deep into it other than the fact that it is essentially rotational data in the form of a Vector3.

1 Like

Thank you, I don’t really work with CFrames and stuff like that, so I was a bit confused

1 Like

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