Locking a Rotation Axis

Hello, I have made a script that gets a dummy to rotate to the direction it is shooting, but the problem is that the dummy also tilts which is unwanted.

Is there any way to maybe lock a certain rotation to stop the dummy from tilting?
https://gyazo.com/f5466be6b0ee60a851b5efcd42dc49bd
Here is the script.

repeat
    local waittime = script.Parent.Cooldown.Value
wait(waittime)
local parts = game.Workspace.Enemies:GetChildren()
local part = script.Parent.Head

local function faceNearestBlock(part, blocks, maxDistance)
    local closestPart
    local minDistance = maxDistance
    for _, partToFace in ipairs(parts) do
        local distance = (part.Position - partToFace.Head.Position).magnitude
        if distance < minDistance then
            closestPart = partToFace
            minDistance = distance
        end 
    end
    if closestPart then
        part.CFrame = CFrame.new(part.Position,closestPart.Head.Position)
    end
end

faceNearestBlock(part, parts, 100)
until script.Disabled == true

You can get the directional vector and make it on a plane (XZ plane), then get a resultant by adding the player’s position to the altered vector - That will be your lookAt point, so just use CFrame.new(pos, lookAt) with the origin as the player’s current position.

Something like this will work:

local dir = (target - current).Unit * Vector3.new(1, 0, 1) -- only gets the x and z component of the vector
part.CFrame = CFrame.new(part.Position, part.Position + dir)

Whereas:

  • Target is the closest head’s position
  • Current is the part’s current position

The is the best I can explain it, lol

5 Likes