Lock on system question

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)

you can use the roblox body movers such as bodygyro to make the character look in the camera direction which will face the enemy

Have you tried using the CFrame.new constructor which will point a part to another part. If I am understanding the question correctly you would just need to do CFrame.new(Vector3 pos, Vector3 lookAt). Using this you can set the characters PrimaryPart(HumanoidRootPart) to lookAt the enemies PrimaryPart.

Here is a useful link on CFrames and all the constructors!
CFrames

Forgot to mention that they actually did make a whole function that replaces the old CFrame.new(Vector3, Vector3) one. You can also choose to do CFrame.lookAt(Vector3 at, Vector3 lookAt, Vector3 up). :slight_smile: , hope this answers your question.

1 Like