Cylinder not rotating correct way due to face rotation

I’ve got a gun that I’m trying to get to point toward a player, however, because of the way the front face of the cylinder is, it follows the player but in the wrong way. Is there a way to rotate the front face so it isn’t sideways, or adjust for this in the CFrame calculation. I only need to gun to follow on one axis. Here’s what I’ve tried.

image

-- base refers to the cylinder, and tHumPart to the player's humanoid root part

-- attempt 1
base.CFrame = CFrame.new(base.Position, Vector3.new(base.Position.X, base.Position.Y, tHumPart.Position.Z)) * CFrame.Angles(0, 0, math.rad(90))

-- attempt 2
base.CFrame = CFrame.new(base.Position, Vector3.new(base.Position.X, base.Position.Y, tHumPart.Position.Z))

-- attempt 3
base.CFrame = CFrame.new(base.Position, tHumPart.Position)

image

Any help appreciated!

3 Likes

Try:

base.CFrame = CFrame.lookAt(base.Position, (tHumPart.CFrame * CFrame.Angles(0, 0, math.rad(90))).Position)

Or:


base.CFrame = CFrame.lookAt(base.Position, (CFrame.Angles(0, 0, math.rad(90)) * tHumPart.CFrame).Position)

You might also need to change tHumPart.CFrame to CFrame.new(tHumPart.Position)

1 Like

Neither work, they have the same behaviour as shown in the video

In studio, select the cylinder part you’re rotating, under the properties window scroll down to PivotOffset and adjust the rotation values as needed. This will allow you to CFrame the part in your script as you already are, but will automatically apply a rotational offset which you can customize to make it so your model is facing the correct way.

Edit - You’ll also need to use the PivotTo(CFrame) method for this to work

2 Likes

The easiest way to solve your problem would be to do this:

  1. Make an invisible cube that is anchored and as big as the whole turret model
  2. Make sure that the cube’s front face is where you want the front of your turret to be (use ShowOrientationIndicator to do so, or a Decal in the front face)
  3. Unanchor everything else exept the cube, and weld everything to the cube
  4. Instead of rotating the base, we will now rotate the cube like so:
cube.CFrame = CFrame.lookAt(cube.Position, Vector3.new(tHumPart.Position.X, cube.Position.Y, tHumPart.Position.Z))

Edit: I made a mistake,

cube.CFrame = CFrame.lookAt(cube.Position, tHumPart.Position)

works much better than the formula I gave you so use it for now while I find a way to stop the turret from rotating up and down unless you do want the turret to do so

3 Likes

Thanks, another solution is to just use a square instead of a cylinder, which is what I did since I don’t really care about the looks too much

1 Like

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