Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to align a part to face a direction using CFrames?

I'm making a rocket, and I decided to make the rocket move forward using raycasting.

However, I ran into a slight problem as you can see in this GIF

This is the code that propels the rocket:

local RayN = Ray.new
local CF = CFrame.new
local HB = game:GetService("RunService").Heartbeat

delay(0, function()
		for i = 1, 500 do
			local Hit, Pos = workspace:FindPartOnRayWithIgnoreList(RayN(Rocket.CFrame.p, (Direction - Rocket.CFrame.p).unit), {IgnoreModel, Character, Tool})
			if Hit then break end
			Rocket.CFrame = CF(Pos)
			HB:wait()
		end
		Rocket:Destroy()
	end)

The problem isn't how fast I can shoot, I'm fixing that later. The problem is the direction the rocket is facing. How would I set the rocket to "look" in the direction of the mouse?

1 answer

✓ Accepted answer

If you are trying to make the rocket face where you clicked, I have a CFrame method for that

p.CFrame = CFrame.new(p.Position, Vector3.new(mhit.x,mhit.y,mhit.z))

mhit stands for mouse.Hit (the position of your mouse) and p stands for the position of where you're firing your rocket from.

If you run into a problem where your rocket is facing the wrong direction but still going where you clicked, you will need to use CFrame.Angles and multiply it by the CFrame.new i gave above, to make it face forward.

Hope this helped!

This discussion is preserved as it was. New answers, comments, and votes are closed.