Hey, so I have a lock on system, it’s a system that basically focuses your camera on the enemy, now, I need to ask, how do I make it so that the players character (avatar) turns to the enemy’s avatar?
Here is my current script:
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local SetLockedRemote = Remotes.SetLockedOn
local Player = Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
local Target = nil
local Range = 250
local function onInputBegan(input, gameProcessed)
if gameProcessed then return end
if input.UserInputType == Enum.UserInputType.MouseButton3 or input.KeyCode == Enum.KeyCode.L then
if not Target then
local MouseHit = Mouse.Hit
local rayOrigin = Character.Head.Position
local rayDirection = ((MouseHit.p - rayOrigin).Unit) * Range
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
local function setTarget(newTarget)
Camera.CameraType = "Scriptable"
Target = newTarget
SetLockedRemote:FireServer(newTarget.Name)
wait()
spawn(function()
while Target and Target:IsA("Model") do
local X = 12
local PartPosition = Target.HumanoidRootPart.Position
local SelfPosition = Character.HumanoidRootPart.Position
local Offset = Vector3.new(5,2,0)
local V = PartPosition + ((SelfPosition + Offset) - PartPosition).Unit * ((SelfPosition - PartPosition).Magnitude + X)
Camera.CFrame = CFrame.new(V, PartPosition)
RunService.RenderStepped:Wait()
end
end)
end
if raycastResult then
local hitPart = raycastResult.Instance
if hitPart.Parent.Parent == workspace.Dummies and hitPart.Parent:FindFirstChild("Humanoid") then
setTarget(hitPart.Parent)
elseif hitPart.Parent.Parent == workspace and hitPart.Parent:FindFirstChild("Humanoid") then
setTarget(hitPart.Parent)
end
end
elseif Target then
Target = nil
Camera.CameraType = "Custom"
SetLockedRemote:FireServer(false)
end
end
end
UserInputService.InputBegan:Connect(onInputBegan)