Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
5

Choppy Aim Script?

Asked by 10 years ago

I am scripting a gun script, I am making it so the gun follows the mouse in first person. I have the basics but it is extremely choppy and I have no idea what the problem is.

local player = game.Players.LocalPlayer
wait(.5)
OFFSET = Vector3.new(0, 1, 1)
local body = player.Character
local mouse = player:GetMouse()
r = false
--Aim Part--
function onmove()
    body.Torso.Neck.C1 = CFrame.new()
    body.Torso.Neck.C0 = CFrame.new(0, 1.5, 0) * CFrame.Angles(math.asin((mouse.Hit.p - mouse.Origin.p).unit.y), 0, 0)
    if body.Torso:FindFirstChild("RightArmWeld") ~= nil
        then
        if r == true
            then
            a = 0
            local dif = body.Head.CFrame:toObjectSpace(script.Parent.Parent.Handle.CFrame).p + OFFSET
            body.Torso.RightArmWeld.C1 = CFrame.new(dif)
        end
        new = (mouse.Hit.p - mouse.Origin.p).unit.y
        x = -1.3
        y = .5
        z = .5
        body.Torso:FindFirstChild("RightArmWeld").C1 = CFrame.new(x,y,z)*CFrame.Angles(-math.asin((mouse.Hit.p - mouse.Origin.p).unit.y)-1.7,0,0)
    end
end
--
function ondown()
    r = true
end
function onup()
    r = false
end
mouse.Move:connect(onmove)
mouse.Button2Down:connect(ondown)
mouse.Button2Up:connect(onup)

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

You should still probably use RenderStepped, by the way.

Anyway, calculate the attitude, or pitch, of the gun to get what angle it is pointing at compared to a vertical line through the character's head.

If an angle of 90* from the vertical is an offset of 0, take the proportion of the angle above and below this point compared to the maximum possible angle in that direction (1* less than straight up or down, iirc), and multiply it by the max (or min) offset to get get how much to slide up or down the arm.

The distortion you're seeing is linear, so you only need a linear offset to fix it.

1
I mean the gun is weirdly placed, the camera is fine. But the guns rotation is great, just the position on the screen in fps moves around. User#5978 25 — 10y
0
Does changing to RenderStepped affect anything? adark 5487 — 10y
0
nope User#5978 25 — 10y
0
Do you have a place currently using this code? I don't understand the problem you're having. adark 5487 — 10y
View all comments (13 more)
0
Are the guns being just broken the problem you're having in your place? adark 5487 — 10y
0
No, They move... Go to the blackhawk rescue place and check there guns and compare to mine, trying to make them not move ;d User#5978 25 — 10y
0
You misunderstand me. The camera in your place is permanently affixed to a specific spot when you use the gun. adark 5487 — 10y
1
Okay... No camera problem....... The gun I want to be a in a fixed place as well as rotate. It rotates but isnt fixed spot. User#5978 25 — 10y
0
Can you upload a gfy of it? I can't get your place to work and I don't understand your problem at all. adark 5487 — 10y
0
go to http://www.roblox.com/Blackhawk-Rescue-Mission-MEGA-VIP-item?id=100737604 and see there gun. Its in a fixed spot at the lower right hand side of the screen, I want my gun to do that as well as point like theirs does. The problem is mine moves when you move the gun. User#5978 25 — 10y
0
Do mean it, like, slides up and down your arm? adark 5487 — 10y
0
Yep User#5978 25 — 10y
0
Are you able to move the gun up or down on the arm via some offset value, and have it stay fixed relative to the arm (not the camera)? adark 5487 — 10y
1
Yep! It already moves up and down but its not fixed :( User#5978 25 — 10y
0
Gonna edit my answer really fast. adark 5487 — 10y
Ad

Answer this question