I’m creating a solo first-person shooter for coding practice and am working on a gun that basically auto-locks onto an enemy NPC’s torso.
Is there any way to figure out where your cursor should point to (regardless of your standing distance) so that it always aims at the body whether you’re 100 blocks away, or 1000 blocks away?
CC = game.Workspace.CurrentCamera
local target = npc.Character:FindFirstChild('Head')
if target then
local temp = target.CFrame.p
local diff = CFrame.new(0, 0, 0) -- difference from head to body points
CC.CoordinateFrame = CFrame.new (CC.CoordinateFrame.p, temp)
end
Absolutely! There is a helpful article called “Understanding CFrames” that will help you out. I’m just going to paste the relevant content here so you can see an example.
——
Facing Toward a Point
One of the most powerful uses of CFrame.new() is the ability to point the front surface of a CFrame at a specific point in the world. Consider the following example which places the redBlock part at 0 , 3 , 0 and then points its front surface (marked by the white circle) at the blueCube part:
local redBlock = game.Workspace.RedBlock
local blueCube = game.Workspace.BlueCube
-- Create a Vector3 for both the start position and target position
local startPosition = Vector3.new(0, 3, 0)
local targetPosition = blueCube.Position
-- Put the red block at 'startPosition' and point its front surface at 'targetPosition'
redBlock.CFrame = CFrame.new(startPosition, targetPosition)