My car’s gun rotates incorrect. It doesn’t look at mobs. But in the end it looks correctly.
I think it’s because my car’s model move and rotate. Mb I need use something like World target position. But I don’t know how to use it.
I use weld to rotate my car’s gun.
Here’s a video:
Here’s a script:
function towerSummons.FaceTarget(newCar, target)
local weld = newCar.GunConnector.Weld
local newCFrame = CFrame.lookAt(newCar.PrimaryPart.Position, target.PrimaryPart.Position)
weld.C0 = newCar.PrimaryPart.CFrame:ToObjectSpace(newCFrame)
end
function towerSummons.FaceTarget(newCar, target)
local weld = newCar.GunConnector.Weld
local inverseCFrame = weld.C0:Inverse()
weld.C0 = CFrame.lookAt(inverseCFrame.Position, target.PrimaryPart.Position):Inverse()
end
From what I can tell, it now rotated only once, which is quite odd, unless the FaceTarget function isn’t being called
If you’re willing to modify the car model, you could use physics constraints instead of welds. You’ll need to use an AlignPosition to position the gun, and an AlignOrientation with its AlignType set to PrimaryAxisLookAt to rotate it. You could also use a HingeConstraint and calculate the angle, but this can get tricky if you want the gun to rotate on more than one plane
@VLASENKO The code that I provided is working correctly for me when I test it, which is why I suspect that the FaceTarget function isn’t being called. I also recommend that you check to make sure that the welds are configured correctly (as in, the Part is welded to the correct Part, and that you’re not editing the weld’s C0 CFrame to affect Part1’s CFrame)