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

[SOLVED] What is the most effecient way to move a character's head up and down?

I had all sorts of lag issues with my guns and I worked so hard to optimize them as much as possible. No matter what I did, something was making the activity rise over 8% when you looked up and down quickly while zoomed in. I finally found it was a method I've been using for a long time now. Moving your arms up and down by simply welding them to the head and moving the head up and down. I tried this:

RunService:BindToRenderStep(Tool.Name.."FPS", Enum.RenderPriority.Character.Value+1, function()
	Neck.C0 = CF()
	Neck.C1 = CF(0, 1.5, 0) * CFAng(MASIN((mouse.Hit.p - mouse.Origin.p).unit.y), 0, 0)
end)

Well, this sorta worked. I'd prefer to keep activity below 4%. Now the activity is 6-7%.

Do any of you guys know any better ways to make the character's head look toward the mouse?

5 comments

0

What about the mouse.Move event?

0

mouse.Move doesn't effect how the head moves. It does effect how it knows when to move the head, but it doesn't effect performance either. I'm looking for performance. Best way to effect that is to change Line 3

0

Maybe then not rendering that many time? RunService.Stepped fires every 1/30(0.03) seconds, which theoretically shouldn't hurt the performance. That would maybe affect the function. I don't think you can change line 3 into something better, or at least I don't know.

0

Of course! I was thinking RenderStepped or Heartbeat or Camera changed, but stepped should work fine. Let me edit the code and run a performance test

0

Well, Stepped helped a bit, it's at a steady 3%... Hoping for a little lower, but I think it can work for now

1 answer

I solved my own question!

What I did was I moved that code to a separate script from the gun. Somehow, binding RenderStepped performs better than Stepped. Here's my final code in a separate LocalScript:

wait(0.25)
Player = game.Players.LocalPlayer
Character = Player.Character
Neck = Character.Torso.Neck
Camera = workspace.CurrentCamera
RunService = game:GetService("RunService")
Tool = script.Parent

Tool.Equipped:connect(function(mouse)
	Turn = RunService.RenderStepped:connect(function()
		mouse.TargetFilter = workspace
		Neck.C1 = CFrame.new()
		Neck.C0 = CFrame.new(0, 1.5, 0) * CFrame.Angles(math.asin((mouse.Hit.p - mouse.Origin.p).unit.y), 0, 0)
	end)
end)

Tool.Unequipped:connect(function()
	Turn:disconnect()
end)

That script runs idling at exactly 2%. When you move your head up and down wildly, it actually gets REDUCED to a little over 1%. Found that extremely weird, but it works.

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