-
What do I want to achieve?
I’m trying to make a lock-on script similar to the one used in SoulShatters (see the video below), I have figured out how to position and orientate the camera properly, however I cannot figure out how to make my character model face the locked-on target’s character properly. I’m currently using an AlignOrientation object to attempt to align my characters orientation to face the character model, and I’m setting the CFrame of the Attachment0 every render step to be the character’s HumanoidRootPart’s position, with it being orientated towards the locked-on character.
Here is the desired outcome:
-
What is the issue?
I cannot figure out how to properly set the AlignOrientation object so that it makes my character model face towards the character that I have locked-on, when I lock onto the character, it’s very glitchy and my character will face a different direction to where it needs to face.
Here is the problem:
-
What solutions have I tried so far?
Before I used an AlignOrientation object, I tried spam setting the CFrame of my HumanoidRootPart, but that was too glitchy to use. Then I tried to use AlignOrientation objects in numerous ways, using both one attachment and two attachments but I cannot figure out how to use it properly.
Any help would be appreciated, and my script is below. Thankyou!
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local mouse = player:GetMouse()
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera
local cameraAttachment = Instance.new("Attachment")
cameraAttachment.Position = Vector3.new(3, 2, 5)
cameraAttachment.Parent = humanoidRootPart
local alignAttachment = Instance.new("Attachment")
alignAttachment.Parent = humanoidRootPart
local AlignOrientation = Instance.new("AlignOrientation")
AlignOrientation.MaxTorque = math.huge
AlignOrientation.Responsiveness = 100
AlignOrientation.Attachment0 = alignAttachment
AlignOrientation.Parent = humanoidRootPart
local lockedCharacter = nil
local locked = false
local function findCharacterModel(part)
local ancestor = part:FindFirstAncestorOfClass("Model")
if ancestor then
if ancestor:FindFirstChildWhichIsA("Humanoid") then
return ancestor
end
end
return nil
end
local function lockOnToCharacter()
local part = mouse.Target
if part then
lockedCharacter = findCharacterModel(part)
if part:IsA("BasePart") and lockedCharacter ~= nil then
locked = true
AlignOrientation.Attachment1 = lockedCharacter.PrimaryPart.RootRigAttachment
repeat
task.wait()
until lockedCharacter == nil
AlignOrientation.Attachment1 = nil
else
unlock()
end
end
end
function unlock()
locked = false
lockedCharacter = nil
camera.CameraType = Enum.CameraType.Custom
end
UserInputService.InputBegan:Connect(function(inputObject, gameProcessed)
if inputObject.UserInputType == Enum.UserInputType.MouseButton3 and gameProcessed == false then
if locked == true and lockedCharacter ~= nil then
unlock()
else
lockOnToCharacter()
end
end
end)
RunService.Stepped:Connect(function()
if locked == true and lockedCharacter ~= nil then
local characterHumanoid = lockedCharacter:FindFirstChildOfClass("Humanoid")
if characterHumanoid then
local modifiedPosition = Vector3.new(lockedCharacter.PrimaryPart.Position.X, humanoidRootPart.Position.Y, lockedCharacter.PrimaryPart.Position.Z)
local newCFrame = CFrame.new(humanoidRootPart.Position, modifiedPosition)
alignAttachment.CFrame = newCFrame
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = CFrame.new(cameraAttachment.WorldPosition, cameraAttachment.WorldPosition + humanoidRootPart.CFrame.LookVector)
end
else
unlock()
end
end)