Rotating model towards mouse position, wired rotations

Hello, I’m trying to make a model rotate towards the mouse position and managed to do it with this code (look code) but I’ve got some wired rotation bugs. Also I want it to rotate slowly towards the mosue position and not instand.

Video:

workspace.T1.Turret.Gun:SetPrimaryPartCFrame(CFrame.new(workspace.T1.Turret.Gun.PrimaryPart.Position, Mouse.Hit.p) *CFrame.Angles(math.rad(0), 0, math.rad(-90)))
1 Like

You could try using body movers to orientate the turret smoothly. This would also eliminate the jittery-ness. For example, you could create a BodyGyro inside your root part in order to set the Orientation of the turret(you would want to use a high D value so that it would be smooth). Then you could create a hinge or other type of constraint to hold the Model to the tank.

I’m sure there are many other ways to do this, such as Lerp()ing or using the TweenService, but this is what I would try first.

Hope this helps!

Very cool tank model by the way!

1 Like

So, I’ve added the BodyGyro to the primarypart of the turret but that didn’t make much different. I can still look down and the gun is still shaking.

BodyGyro D: 10000000000 (I have tried lower values as well)
Video: https://www.youtube.com/watch?v=mPsyF5XNR88&feature=youtu.be

Hmm… Did you try lowering the P and MaxTorque properties?

You can change the MaxTorque of the gyro so the gun can’t move on the Y-Axis.

BodyGyro.MaxTorque = Vector3.new(math.huge, 0, math.huge)
1 Like

Lowering the P didn’t do anything

Hi, just use for Y position your Y part position

 local mousePosition=Mouse.Hit.p
 local orientationDirection = Vector3.new(mousePosition.X, workspace.T1.Turret.Gun.PrimaryPart.Position.Y, mousePosition.Z)
 workspace.T1.Turret.Gun:SetPrimaryPartCFrame(CFrame.new(workspace.T1.Turret.Gun.PrimaryPart.Position, orientationDirection) *CFrame.Angles(math.rad(0), 0, math.rad(-90)))
1 Like

After adding the code to the while loop the gun rotates smooth but is still rotating in the Y axis

while wait() do

workspace.T1.Turret.Gun:SetPrimaryPartCFrame(CFrame.new(workspace.T1.Turret.Gun.PrimaryPart.Position, Mouse.Hit.p) *CFrame.Angles(math.rad(0), 0, math.rad(-90)))
workspace.T1.Turret.Gun.Hud.BodyGyro.MaxTorque = Vector3.new(math.huge, 0, math.huge)

end

Thank you so much, replacing that actually fixed it

1 Like

Going a little off topic but how can I not make it rotate instand, like @ExcessEnergy said lerping for example. But I’m gonna need to send the gun’s CFrame over events to the server so the lerp needs to be updated and overrided everytime the mouse moves ?

You can use Network Ownership
Network Ownership | Documentation - Roblox Creator Hub

Already love this, thanks again. I didn’t know there was a way to bypass the events this will be a big help towards developing my game.

2 Likes

You could try increasing the mass of the total mass of the turret.