# Making a welded part look at another part

Hey guys, bit of a mathy question here.

I currently have a hitbox on an enemy, and the enemy model welded to the hitbox.

Now, the enemy is welded with offsets to its cframe - it’s rotated 90 degrees, and moved back 4 studs on the z axis.

Would anyone be willing to help me figure out how to keep the offsets, while also getting the enemy model to look at a specific position - for example the player?

I’m assuming this is going to require CFrame:Inverse(), but I am pretty poor at handling cframe maths like this. Any help would be greatly appreciated!

The offsets for the model are currently set to the weld’s C0.

( weld.C0 = CFrame.new(0,0,-4) * CFrame.Angles(0,math.rad(90),0) )

1 Like

if the hitbox is parented to the enemy model then it should keep its relative positioning when the enemy moves or if the model is moved with :PivotTo()

1 Like

It’s actually the opposite - the model is parented to the hitbox, and is only visible on the client. I am doing this client-sided for optimization.

I would just use :PivotTo() on the hitbox to make the hitbox look at the player, but when I do that it locks the hitbox in place, and I want the enemy to still be able to move around. I also want the enemy model to be facing towards the player even if the enemy’s hitbox is facing a different direction.

So, I figured just finding out the math to adjust the welds offsets would be the way to go, no?

1 Like

Sorry im not sure exactly what youre trying to do. do you want the hitbox to be fixed 4 studs behind the enemy’s back no matter which way the enemy is facing?

1 Like

Another way to imagine the problem would be, let’s say there’s a turret welded onto a tank.

As the tank drives around, I would want the turret to remain rotated towards an enemy target.

What sort of math would be involved in that?

oh ok i think i understand. So the hitbox keeps a fixed orientation in space (while being positioned on the enemy) while the enemy can move around and turn to face the character?

1 Like

Yes, that’s what I am trying to achieve.

Ok try this

``````local baseOffset = CFrame.new(0,0,-4) * CFrame.Angles(0, math.rad(90), 0)
weld.C0 = Enemy:GetPivot().Rotation:ToObjectSpace(baseOffset)
``````

this is what you would set the cframe to every render step or whatever

1 Like

Hmm. I did manage to work it out after messing with your code a bit, but I ran into some really weird behavior, so I don’t think this is the solution.

The enemies will rotate towards the player but… it seems to depend on the Hitbox’s rotation, still. Like keeps the rotation offset of the hitbox to the player, and uses that to determine the new rotation. Hard to describe what’s going on.

I used

``````local lookAtCFrame = CFrame.new(StartPosition, lookAt)
lookAtCFrame = lookAtCFrame - lookAtCFrame.Position
Weld.C0 = lookAtCFrame:ToObjectSpace(WeldOffsetCFrame)
``````
1 Like