I am trying to make a gun framework and cannot work out how to make the gun aim if the player holds right click. i know i need to use UIS/Mouse which i have done but i cant get it to smoothly animate to the aim pos, or even aim at all.
my gun is in
workspace.CurrentCamera
so i am using RenderStepped
How would i do this? i am trying to make the aiming similar to the ones in phantom forces
3 Likes
mobyboyy
(mobyboyy)
September 26, 2019, 6:17pm
#2
Try looking at this resource: The First Person Element Of A First Person Shooter
EgoMoose:
Aiming down sights
To get our weapon to aim down the sights we will add a small invisible part to our weapon called Aim
. We will use this part as a reference to where the weapon should be attached to the head when the player is aiming. Again, we’ll make sure the front face is facing forward and the up face is facing up.
Since we adjusted the C0
value earlier we will make this adjustment in it’s entirety with C1
to avoid overlap.
To start we use the basic equality of joints we can figure out how to pick C1
given that weapon.Handle = joint.Part1
and we’re setting joint.Part1.CFrame = camera.CFrame
.
joint.Part0.CFrame * joint.C0 == joint.Part1.CFrame * joint.C1
joint.C1 = joint.Part1.CFrame:inverse() * joint.Part0.CFrame * joint.C0
-- recall though that joint.Part0.CFrame == camera.CFrame, thus:
joint.C1 = joint.C0
Of course we want to further adjust this so the camera focuses on the Aim
part, not Handle
. So using inverses we can find the offset that would be needed to move from the Handle.CFrame
to the Aim.CFrame
.
Handle.CFrame * offset = Aim.CFrame
offset = Handle.CFrame:inverse() * Aim.CFrame;
Putting this all together we get:
local aimCount = 0;
local offset = weapon.Handle.CFrame:inverse() * weapon.Aim.CFrame;
local function aimDownSights(aiming)
local start = joint.C1;
local goal = aiming and joint.C0 * offset or CFrame.new();
aimCount = aimCount + 1;
local current = aimCount;
for t = 0, 101, 10 do
if (current ~= aimCount) then break; end
game:GetService("RunService").RenderStepped:Wait();
joint.C1 = start:Lerp(goal, t/100);
end
end
local function onInputBegan(input, process)
if (process) then return; end
if (input.UserInputType == Enum.UserInputType.MouseButton2) then
aimDownSights(true);
end
end
local function onInputEnded(input, process)
if (process) then return; end
if (input.UserInputType == Enum.UserInputType.MouseButton2) then
aimDownSights(false);
end
end
game:GetService("UserInputService").InputBegan:Connect(onInputBegan);
game:GetService("UserInputService").InputEnded:Connect(onInputEnded);
3 Likes
hmm, time to get to rewriting almost my entire system
1 Like
mobyboyy
(mobyboyy)
September 26, 2019, 6:35pm
#4
Just use the math as a reference for the aiming, not rewrite the entire thing. I quoted the main section you will wanna use.
1 Like
mobyboyy
(mobyboyy)
September 26, 2019, 6:40pm
#5
You can just use this function.
EgoMoose:
local aimCount = 0;
local offset = weapon.Handle.CFrame:inverse() * weapon.Aim.CFrame;
local function aimDownSights(aiming)
local start = joint.C1; local goal = aiming and joint.C0 * offset or CFrame.new();
aimCount = aimCount + 1;
local current = aimCount; for t = 0, 101, 10 do
if (current ~= aimCount) then break; end
game:GetService("RunService").RenderStepped:Wait();
joint.C1 = start:Lerp(goal, t/100);
end
end
3 Likes
ok, thank you. i will see if it works
2 Likes
Swatdude22
(Swatdude22)
December 8, 2022, 10:28pm
#7
whats the joint part supposed to mean, I dont understand.
You can use a ModuleScript and state the AimSpeed, then in a localscript in StarterPlayer you can do:
local aimCF = CFrame.new()
if isAiming and framework.viewmodel ~= nil and framework.module.canAim then
local offset = framework.viewmodel.AimPart.CFrame:ToObjectSpace(framework.viewmodel.PrimaryPart.CFrame)
aimCF = aimCF:Lerp(offset, framework.module.aimSpeed)
end