How would I make a turret face the mouse of a player?

Hey!

I’m currently scripting for someone a turret that can move when the player has moved their mouse. However, I can’t seem to be able to make the Turret turn and face the player’s mouse. Whenever I do try, it either does not work at all or the whole plane that the turret is in moves along with the turret. Someone, please help me out.

Here are some screenshots of the model along with my code:

image
image

The part that I most need help within the code is:

image

And this is what happens when the Turret does decide to move:

If you think you can fix this issue, can you please reply to this post? Thanks.

You need a remote event to send the information to the server.

I would recommend to render movement on the original client first, then send the mouse hit to the server, which sends it to each client, and if that client isn’t the original client, render the movement.

Edit: Also, for the CFrame, just do CFrame.new(origin, mouse.Hit.Position)

1 Like

The thing is, I have already tried to make a remote event that sends over the info to the server. But when I tried actually making use of the info (which was the position of where the mouse hit) and try to change the CFrame of the turret, it either wouldn’t work or the WHOLE plane itself would move.

I’m guessing it’s welded then? You could use joint manipulation for this (motor6Ds, regular weld c0s, etc.)

Edit: there might be a better way to do this with physics but i’m not an expert on physics so shrug.

2 Likes

The turret itself is all anchored. I’m not the best at motors and welds. I to be honest just wanna find a way where the turret will move freely within the X and Y axis without the problem happening in the vid.

Does the plane actually move?

If the plane was welded to the anchored turret, it would still move the plane too.

1 Like

Yes the plane does actually move.

I have tried moving the plane EVEN with the anchored turret and it somehow worked. The turret was constantly welded into the plane with no problems :confused:

This sounds pretty finickey, I would think you’ll run into a lot less issues if you try one of the methods I mentioned above with the turret unanchored.

1 Like

I don’t think the turret is anchored while the plane is moving. Do you have a WeldScript somewhere in this beast that welds the Parts and Unanchors them?
As stated I’d make the turret unanchored and put it on a HingeConstraint with the Motor set to motor or servo, then use the information from your mouse to spin the motor.
Btw, gorgeous looking B-17!

2 Likes

Now that I have looked over the script, I’ve noticed it.

There is a weld script inside the plane:

I’ll try using motors and hinges. The B-17 was made by @GIennMiller , credits to him for the plane, I’m just scripting the turrets for him lol

1 Like

Well first, that script most likely unanchors all parts that it effects as mentioned by @Scottifly. second, it’s not using weld constraints. This is usually bad but in your case you can use this to your advantage and edit the C0 of the welds.

Just make sure the other parts are weld constrainted to the center part moving in the turret.

1 Like

To elaborate on the C0 editing, you can change the rotation and position offset of the weld, allowing you to move the welded part when the plane moves, and independently at the same time

1 Like