Adjusting bone to point towards player

This is very similar to my previous post, but this model uses bones instead of parts

I’m trying to get this model to rotate and point towards a player. Currently, I am adjusting the CFrame of the entire part to point torwads the player (which is sorta a workaround, but it works), but now I need a bone inside of the mesh to point towards the player.

Now I need this specific bone, called “GunBase” to rotate and point towards the player too.

Here’s my current script, similar to the script in the previous post, and this is the behaviour I get.

-- part refers to mesh, thumpart refers to the humanoid root part of the player, and gunbase to the bone
part.CFrame = CFrame.new(part.Position, Vector3.new(tHumPart.Position.X, part.Position.Y, tHumPart.Position.Z))
gunBase.CFrame = CFrame.new(gunBase.Position, Vector3.new(tHumPart.Position.X, part.Position.Y, tHumPart.Position.Z))

This is what the GunBase bone looks like at neutural

@JohhnyLegoKing @BuilderDolphin @creepersaur You guys were helpful on the last one, were wondering if you had any ideas when it comes to these bones

1 Like

Alternatively, I was thinking maybe I could attach the bone to a separate part, and then change the CFrame of that part. Don’t know how that would work though

Tried trying something from this post, and got this:

part.CFrame = CFrame.new(part.Position, Vector3.new(tHumPart.Position.X, part.Position.Y, tHumPart.Position.Z))
					
local gunPos = gunBase.Position
local lv = tHumPart.CFrame.LookVector
local dist = (gunPos - tHumPart.Position).Magnitude
local diff = gunPos.Y - tHumPart.Position.Y
					
gunBase.CFrame = CFrame.Angles(0, 0, (((gunPos-tHumPart.Position).Unit):Cross(lv)).Y)

It almost seems to be working, but not quite right and definitely in the wrong positions. Timesing the CFrame.Angles to the current CFrame wouldn’t work either as that just makes it spin around in circle.

Is the bottom half of turret on a hinge and unanchored?

if not, try CFrame:LookAt().

1 Like

Or, you can try reversing and changing positions until the orientation is correct

1 Like

No, they are all bones. This is the hierachy here
image

I don’t use bones, so I don’t know anything about them, so I recommend just trying to change vector3s until it points correctly.

Still looking for help with this if anyone

maybe try bone.Cframe = Cframe.New(bone.Position, targetTorso.Position)

Bumping this thread because after SO MUCH TIME I have found a solution! I don’t know if this will work for others, but it worked for me.

You can use Cframe.lookAt() to make the bone face a part, but for bones you MUST USE WorldCFrame, as using only the bone’s CFrame will set it relative to the bone, and not the world axis. For example, your script would look like:

gunBase.WorldCFrame = CFrame.lookAt(gunBase.WorldCFrame.Position,tHumPart.Position)

If it’s facing the wrong direction of if its just off by a few degrees, you can use
CFrame.Angles() to adjust an offset like so:

gunBase.WorldCFrame = CFrame.lookAt(gunBase.WorldCFrame.Position,tHumPart.Position) * CFrame.Angles(0, math.rad(180), 0) 

It’s such a stupid mistake but I was completely unaware WorldCFrame!! I hope this helps you or anyone else who may come across this!

1 Like