Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Choppy Aim Script?

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

✓ Accepted answer

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.

17 comments

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.

0

Does changing to RenderStepped affect anything?

0

nope

0

Do you have a place currently using this code? I don't understand the problem you're having.

0

Are the guns being just broken the problem you're having in your place?

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

0

You misunderstand me. The camera in your place is permanently affixed to a specific spot when you use the gun.

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.

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.

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.

0

Do mean it, like, slides up and down your arm?

0

Yep

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)?

1

Yep! It already moves up and down but its not fixed :(

0

Gonna edit my answer really fast.

This discussion is preserved as it was. New answers, comments, and votes are closed.