Rookie CFrame Problems

Essentially I’m making a FireGun System Where when you click you shoot a tiny fire pellet out of your left hand and then on the next click it’ll shoot from your right.

The Issue is that the Tween will follow where the hand is pointing and so if you Commit to an action like jumping for example the FireGun shoots directly towards the sky rather than Infront of you


Im looking for a way to be able to spawn a CFrame to a place and then tween it without it taking the part it was CFramed on into Consideration

Heres my client code


And the server code
image

3 Likes

The simplest fix would probably be to orient around the HumanoidRootPart rather than the arm. That way it won’t be disturbed by any arm flailing animations. Just apply an arbitrary local offset to get it to fire from where it should.

Something like

local OffsetVector = Vector3.new(2, 0, 0)
local RootCFrame = HumanoidRootPart.CFrame
local StartPosition = RootCFrame:PointToWorldSpace(OffsetVector)
local Direction = RootCFrame.LookVector

If you’re following my thinking here

2 Likes

I getcha thanks im gonna try it right now

I think i may have done somthing wrong
While im facing forwards it (kinda) correctly shoots forward
However the moment i turn to my side it stops


image
(I changed Visuals to equal the HumanoidRootParts Cframe)

Would you want it coming out of the tip of the arm but oriented toward in the torso’s direction?

This is because you’re adding the offset in world space rather than object space. As I mentioned, you’d want the offset to be local. If that concept is new to you, there’d of course be other ways of going about building a new CFrame with the arm’s position along the rootpart’s direction—but I’m figuring you probably wouldn’t want the anims interfering with the start pos either.

2 Likes

Yeah that would be preferred so that the player can aim it towards a person better

Yeah I’ve never heard of object space I’m gonna do a little research on it.

local x,y,z = Torso.CFrame:ToOrientation()
Goal.Position = (LeftArm.CFrame:ToWorldSpace(CFrame.new(0,0,-1)).Position * CFrame.Angles(x, y, z)):ToWorldSpace(CFrame.new(0, 0, -500)).Position

Something like this sorry if it doesn’t work I’m on mobile.
You’ll have to fill in the Torso and LeftArm with their respective parts. Sorry that I can’t make that script modification for you I just can’t type that much.

Basic rundown of what it does:
leftarm.cframe:ToWorldSpace(CFrame.new(0,0,-1)).Position - gets the cframe to the tip of the arm

CFrame.Angles(x, y, z) - applies the torso’s orientation onto our current cframe

:ToWorldSpace(CFrame.new(0, 0, -500)).Position - makes the goal far away and makes it a position

EDIT: this won’t really work since it’s from the tip of the arm. If it still isn’t solved by tomorrow then I could by then.

1 Like

A CFrame is composed of four vectors. A position, and then three other directional vectors (RotX, RotY, RotZ), which together describe rotation. LookVector, which you seem to be familiar with, is just that third rotational vector, only pointing in the opposite direction (‘forwards’ as opposed to ‘backwards’).

So, if I have a LookVector that’s [-0.5, 0.3, 0.2], what that’s essentially saying, is that for every stud I want to move in the direction that the object is facing, I have to move -0.5 studs on the global X axis, +0.3 studs on the global Y axis, and +0.2 studs on the global Z axis.

And this is fundamentally what traveling in ObjectSpace is. If I want to move an object forwards by 5 studs, I multiply its CFrame’s RotZ vector by -5, and then add the resulting vector to its position.

Offsetting an object by a vector is much the same. An offset of [2, -4, 6], means multiplying RotX by 2, multiplying RotY by -4, multiplying RotZ by 6, adding each of these resulting displacements together, and then finally adding the object’s position. And this is exactly what happens when you multiply a CFrame by a Vector3. CFrame * Offset is analogous to CFrame:PointToWorldSpace(Offset). They are the same operation.

Thus, as I wrote in my original reply,

And if we want to convert this into a CFrame quantity, such that the bullet faces the same direction as RootPart, then we can just grab those rotational vectors from RootCFrame.

local StartingCF = CFrame.fromMatrix(RootCF * Offset, RootCF.XVector, RootCF.YVector, RootCF.ZVector)
-- Or more briefly,
local StartingCF = CFrame.fromMatrix(RootCF * Offset, RootCF.XVector, RootCF.YVector)
-- As specifying RotZ is unnecessary, since it can already be inferred from which way RotX and RotY are facing.
2 Likes

Alright so after following your guide it works perfectly.

However my problem is that while i know how to get it to work I’m not confident that i fully understand why it works.

Do you have any recourses that i can use to study CFrames more because i feel like I’m going to run into a lot of problems like these as i continue.

And thank you so much for your help!

1 Like

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