How do I make a part rotate according do the direction the player is facing?

  1. What do you want to achieve? I want to make a bullet that appears in front of the player (I have achieved this) and the bullet the face the direction the player is facing.

  2. What is the issue? The bullet will appear in front of the player as planned, the only problem is that the bullet is turned opposite to the player (Horizontal rather than Verticle). Here’s a video of the issue.

  3. What solutions have you tried so far? So far I’ve tried adding with multiple different Vector3 operations (I made sure to add it to the bullet) as well as multiply with CFrame angles.

This is in a server script in Server Script Services. The Positioning and rotating are on lines 49 to 52

local Time = 6
local e = 5

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local M6D = Instance.new("Motor6D", char.Torso)
		M6D.Name = "ToolGrip"
	end)
end)



local sound = game.ReplicatedStorage.Sounds.TommyGun 
local sounddos = game.ReplicatedStorage.Sounds.Stab
game.ReplicatedStorage.JHamonReps.GunHit.OnServerEvent:Connect(function(player, humanoid)
	local ssound = sounddos:Clone()
	ssound.Parent = humanoid.Parent.Head
	ssound:Play()
end)

game.ReplicatedStorage.JHamonReps.GunHit.OnServerEvent:Connect(function(player, humanoid)
	if humanoid.Health >= 1 then
		humanoid.Health = humanoid.Health - 1
	elseif humanoid.Health < 1 then
		humanoid.Health = 0
	end
end)

game.ReplicatedStorage.JHamonReps.AttachGun.OnServerEvent:Connect(function(plr,location)
	local csound = sound:Clone()
	csound.Parent = plr.Character.Head
	csound:Play()
	local bt = game.ReplicatedStorage.JHGun:Clone()
	bt.Parent = plr.Character
	plr.Character.Torso.ToolGrip.Part0 = plr.Character.Torso
	plr.Character.Torso.ToolGrip.Part1 = bt.BodyAttach
	local pp = Instance.new("Part")
	pp.Transparency = 1
	pp.Parent = plr.Character
	
	while Time > 0 do
		if e < 1 then
			pp:Destroy()
			e = 5
			pp = Instance.new("Part")
			pp.Transparency = 1
			pp.Parent = plr.Character
		end
		local be = game.ReplicatedStorage.Bullet:Clone()
		be.Parent = plr.Character.Part
		be.CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.LookVector*10 
		be.CFrame = be.CFrame + Vector3.fromAxis(Enum.Axis.X, 90)
		e = e -1
		wait(0.1)
		Time = Time - 0.08
		plr.Character.Part:WaitForChild("Bullet").Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid ~= plr.Character.Humanoid then
				if plr.Character.Humanoid.Health > 0 then
					if hit.Parent.Humanoid.Health >= 1 then
						hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 1
					elseif hit.Parent.Humanoid.Health < 1 then
						hit.Parent.Humanoid.Health = 0
					end
				end
			end
		end)
	end 
	pp:Destroy()
	csound:Stop()
end)

game.ReplicatedStorage.JHamonReps.DetachGun.OnServerEvent:Connect(function(plr)
	plr.Character.JHGun:Destroy()
	plr.Character.Torso.ToolGrip.Part1 = nil
end)
2 Likes

You can use some cframe math to rotate and move forward by half the bullet’s width (assuming it’s long on the X axis).

be.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.Angles(0, math.rad(90), 0) * CFrame.new(be.Size.X / 2, 0, 0)
3 Likes

What you can do is use CFrame.lookAt(origin, direction) to accomplish this. First, de-unitize the look vector, since look vector is unitized, you can just do vector * magnitude to get the actual vector. Then, simply offset the result by the humanoid root part’s position and you get the direction.

Then, simply put those values into CFrame.lookAt:

local direction = plr.Character.HumanoidRootPart.Position + plr.Character.HumanoidRootPart.CFrame.LookVector * plr.Character.HumanoidRootPart.Position.Magnitude

be.CFrame = CFrame.lookAt(plr.Character.HumanoidRootPart.Position, direction)
4 Likes

very helpful, thank u from 11 months later lol

1 Like