Need help with what code modifications to do (mainly about CFrames)

What do I want to achieve?
I want to make a spear throw move and the spear to be rotated like this:

What is my problem?
My problem is that the spear is oriented this way:


The spear is oriented this way because

^ this IS the model’s “Orientation 0,0,0”

^ NOT this one (this isn’t a model’s “Orientation 0,0,0”)

Possible help for solutions I’m looking for:
I know 2 solutions to this problem:

  1. Change the model’s “Orientation 0,0,0” (will probably require a plugin)
  2. Add to my code to change the origin (hard because the rotation is by CFrame as this projectile is aimed by mouse cursor)

For this post, I or we would only be on about solution 2.

What CFrame code modifications should I do to this?

local function R1(plr, mouse)
	local spear = game.ServerStorage.AetherSpear:Clone()
	spear.Parent = workspace
	spear.Position = plr.Character.UpperTorso.Position
	spear.CFrame = CFrame.new(plr.Character.HumanoidRootPart.CFrame.p,plr.Character.HumanoidRootPart.CFrame.p+mouse) --this is the main/most important line
-- anything below this line isn't important
-- there was movement code around here, but i decided to hide it
end

local R = {R1}

game.ReplicatedStorage.Attack.OnServerEvent:Connect(function(plr, keything, mouse)
	local kit = plr.CurrentKit.Value
	local enukey = Enum.KeyCode
	if keything == enukey.R then
		R[kit](plr, mouse)
	end
end)

--mouse = GetMouse:().Hit.LookVector

Cframe.new(posA, posB) will make the front of your part face from posA toward posB.
But the true front of your spear mesh isn’t where you’d expect it to be.
To compensate for that, just rotate the spear afterward.
I tried it out and the spear’s orientation should be (0, 90, 90) to make the spear’s top instead of its true front face face forward.

local spearOrientation = CFrame.fromOrientation(0, math.rad(90), math.rad(90)) -- watch out for the radians, you can't just put (0, 90, 90) here. also, this line can be outside the function, it does not need to be recalculated every time you create a new spear
-- ...
spear.CFrame = CFrame.new(plr.Character.HumanoidRootPart.CFrame.p,plr.Character.HumanoidRootPart.CFrame.p+mouse) * spearOrientation

That’s pretty much it.

spearOrientation is just a rotation with no position. You can multiply a CFrame with this rotation to rotate it.
If you set a part’s CFrame to spearOrientation, it’ll face the same way you put the spear in the second image, but at a position of (0, 0, 0).


I can’t leave without nitpicking a little, though.
That line should look like this:

spear.CFrame = CFrame.new(plr.Character.HumanoidRootPart.CFrame.Position, mouse) * spearOrientation

if mouse refers to the position of the mouse (i.e. mouse.Hit.p or mouse.Hit.Position).
I’ve made it just point to mouse because that other stuff just deflects it in an unrelated direction.
The spear should point from the root part toward the mouse, and nothing else.
In addition, CFrame.p is deprecated in favor of CFRame.Position.

1 Like