Making a motor6d point in direction of mouse

Hello. I’ve been trying to make a model (turret) point in the direction of my mouse using a motor6d. I’m using a motor6d because the turret is mounted on a part. I’ve tried to code this in multiple different ways, which are shown below:

First Method

local hit = mouse.Hit
local direction = hit.Position + (hit.LookVector * 5000)
	
local jetCFrame = workspace.Part.CFrame
local rotationOffset = (jetCFrame - jetCFrame.Position):Inverse()
	
-- I tried each of these lines of code separately; neither work
turretMotor.Transform = rotationOffset * CFrame.new(Vector3.new(0, 0, 0), direction)
turretMotor.C0 = CFrame.new(turretMotor.C0.Position, hit.Position)

However, this causes weird behavior shown in the video below:
robloxapp-20220907-1231160.wmv (1.6 MB)

Other Method
I also tried this method for rotating the turret only along the y axis, but it causes problems when the beta variable is not between -90 to 90:

local turretOriginY = Vector2.new(turret.Stand.Position.X, turret.Stand.Position.Z)
local turretGoalY = Vector2.new(mouse.Hit.Position.X, mouse.Hit.Position.Z)
local ratioY = (turretGoalY.Y-turretOriginY.Y)/(turretGoalY.X-turretOriginY.X)
local betaY = math.atan(ratioY) 

turretMotor.C0 = offset * CFrame.Angles(0, betaY, 0)

This video shows the problem with this code (it snaps back to -90 or 90 when it’s not within that range):
robloxapp-20220907-1234363.wmv (271.0 KB)

Can anyone help me point this turret towards the mouse with motor6ds? Thank you!

Hello, I’ve been trying some different things and none of them were successful. If anyone can help, I’d appreciate it.

1 Like

you can simply do something like

turrentM6D.C1 = CFrame.new(turrentM6D.C1.Position, mouse.hit.p) --I'd personally use C1 because it doesnt affect the joints actual position.
--put this is a loop of some kind to make it constant.

CFrame.new() would be the best way to do this because of its parameters. Its kind of confusing to explain on my end but-

CFrame.new(position, lookAt)

position is where the object is located, lookAt is what the object is looking at.

Thanks for the reply! I’ve already tried this method, which I showed in my first code snippet. Unfortunately, it gives some unexpected behavior, which can be seen in the first video.
Using CFrame.new() works when there are no motor6ds or other parts, but once I add a motor6d it acts odd.

1 Like

have you maybe tried reversing the values by puting a negative infront of it? It might work. you gotta experiment with stuff like this lol.

M6D.C1 = -(CFrame.new(pos,lookat))

couldn’t you just use the lookAt property of CFrame?

turret.CFrame = CFrame.lookAt(turret.position, mouse.hit.position)

of course, bind this to a loop if necessary

1 Like

You cannot negate a CFrame, but I have tried multiplying by CFrame.Angles values that I thought might work, and negating pos and lookat. However, I’ve had no success.

What didn’t work for CFrame.lookAt?

I believe that using lookat will do something very similar or the same. Either way, I’ve tried it and it doesn’t work.

Yes, I’m trying to figure out why it doesn’t work. Are you trying to set the CFrame of the model? Is the script erroring?

In the model, everything is welded to the primary part.
I placed the Motor6D inside the part that the turret is attached to.
C0 of the Motor6D is the primary part of the turret. C1 is the part that the turret is attached to.

I understand, but this still doesn’t explain what happened when you tried to use CFrame.lookAt.

What you could do is: Turret:SetPrimaryPartCFrame(CFrame.lookAt(Turret.PrimaryPart.Position, Mouse.Hit.Position))

Please note that SetPrimaryPartCFrame is deprecated, so I can’t encourage you to use this, even though the impact on performance may be slim to none. The correct alternative would be Model:PivotTo(Cframe value), which you can read about here: PVInstance:PivotTo

I don’t believe that function will work with a part connected to a motor6d. I need to directly access the motor6d and change the CFrame from there via Motor6d.Transform or Motor6d.C0.

2 Likes