Help needed with CFrame and Motor6D

Hello!

I need some help with rotating a gun. I’m not using a tool, i made a custom module.

This is my gun, i wanna rotate it so it’s facing forward:
image

This is my current script. I’ve tried using Welds, but some people said that Motor6D was more effective. Eventually this also didnt work.

image

Thanks for reading! I could really use some help.

2 Likes

Motor6D.DesiredAngle is only for when you’re using the motor as a motor, to spin something. You need to set the Motor6D.C0 or C1 to get a rotational offset. Also, math.acos() is not what you want, you need math.rad(45) to rotate 45 degrees. And it looks to me like you need more like a 90 degree rotation.

Thanks for your response!

I changed a few things, and here’s the result:
image

Unfortunatly, this didn’t work.

It didn’t work because you’re still trying to use DesiredAngle, which isn’t going to ever work, and doesn’t take a CFrame (just a number). You need to set C0 not DesiredAngle.

Alright, i did it like this:

image

But now the gun (a meshpart) is on a very weird position. I can’t see it.

I also have these welded, but theyre close to each other.

image

Here are two ways to do it:

Easy way: Position the weapon using animations, no CFrame knowledge required it doesn’t matter if you don’t use a tool it’s still a Motor6D and Motor6D’s are animateable.

Hard/advanced way. Study CFrames, Motor6Ds and welds follow the Joint Instance formula to keep two parts connected together with an CFrame offset, I talked about it here:

Weld/Joint Instance/Motor6D formula:

part1.CFrame * C1 == Part0.CFrame * C0

Honestly the main usage of this knowledge is to position the Motor6D in world terms so you can position a Motor6D part 1 like you would with a normal part by setting part.CFrame = CFrame.new(something) without weird stuff happening.

I recommend experimenting with this formula by @Rare_tendo and seeing how it works.

motor.C0 = motor.Part0.CFrame:inverse() * TargetCFrame

Using this formula you will be able to position motor6D part1 equal to setting a part without disconnecting the Motor6D

someMotor6D.Part0 = anotherPart -- doesn't matter gets inversed away
someMotor6D.Part1 = gunPart --some random part

someMotor6D.C0 = someMotor6D.Part0.CFrame:inverse() * CFrame.new(5,5,5)
--The above CFrame will position and orientate the gun part equal to this no matter the part0 position and orientation as it has been "Inversed":
--gunPart.CFrame = CFrame.new(5,5,5) -- at position 5,5,5

Hope this helps you get started with learning how to position Motor6Ds with one additional CFrame formula as a tool under your belt. For your gun example try doing this though:

@TheRealNcMaster try this one out

wait(5)--dirty use wait for child later please
local rightHand = character.RightHand
local leftHand = character.LeftHand
local positionGunCFrame = CFrame.lookAt(rightHand.Position,leftHand.Position)--make it position at right hand looking at left hand position
--since you still named it weld
--Use the CFrame formula
weld.C0 = weld.Part0.CFrame:inverse() * positionGunCFrame 
2 Likes

That’s because C0 is an offset and character.RightHand.CFrame is the world space position of the character’s hand. So if the character is 1000 studs from the origin, the gun is going to be 1000 studs away from their hand… not what you want. Start by losing the translation term altogether and just setting C0 equal to the Angles CFrame. Once you have the angle right, you can go about adding just enough translation to position the handle in the hand.

1 Like

Nope, this has the same sort of problem he’s already got. C0 is a relative offset, and someMotor6D.Part0.CFrame:inverse() is an offset that’s as large as the character’s hand is far from the world origin. The C0 or C1 you need is going to have a tiny translation, the distance from the center of the gun to where the grip handle is.

2 Likes

I changed my code again and this is the result:
image
image

I think i understand what you mean, i’ll try it out.

Yes, because you’re still setting an offset property (Weld.C1) to a world-space position of the character or gun. Like I said, definitely not what you want, that offset is going to be as large as the distance your character is from 0,0,0 in the world.

If your gun is at positon 0,0,1000 in world space, then weld.Part1.CFrame:Inverse() is going to have a 1000-stud translation, making your gun be 1000 studs from the character.

This worked, thanks!

image

image

4 Likes

Yep thats true, it will offset the Part1 towards the world origin at CFrame.new().

However it’s possible to have that tiny C1 or C0 by adding another CFrame operation to reposition the part1 towards the players hand like so and reverse that offset:

Personally I like this method as it allows me to use CFrame.lookAt with world terms like righthand.Position and much more, but yeah are definitely other more maybe more suitable and efficient CFrame operations to do the job.

I’ll leave this method up since I use it a lot in my projects. Another one I like is this:

local part0CFrame = weld.Part0.CFrame
local undoOrientationOffset = (part0CFrame-part0CFrame.Position):Inverse()

Also, @TheRealNcMaster please make sure to mark @EmilyBendsSpace post as the solution and next time use three back ticks ``` to format your code so others can modify and fix it more easier.

--```
print("Hello World")
--```