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

How would I keep a part at a certain distance from the player's head at all times?

I'm making a script that allows you to pick up certain parts by pressing 'E'. The object will then hover in front of the player's head (locked to their mouse as I tried to use the head and couldn't figure it out), and the player then can choose to drop it by pressing E again, like in Half-Life or Portal. But, I've hit a roadblock. The object will keep flying into the camera, and if the player points at the sky (for example), the object will go too far out. Basically, how can I lock it at a certain distance but maintain it's ability to follow the mouse? (Background info: Player will be in First Person)

local object
local Player = game.Players.LocalPlayer
local Character = Player.Character
local PlrMouse = Player:GetMouse()
local Camera = game.Workspace.CurrentCamera

while true do
	wait()
	while script.Parent.Grabbing.Value == true do
		object = script.Parent.Obj.Value
		object.CanCollide = false
		wait()
		rot = Vector3.new(PlrMouse.Hit.X, PlrMouse.Hit.Y, PlrMouse.Hit.Z)
		object.Position = rot
	end
end

2 comments

2 answers

✓ Accepted answer

You say you want the part to hover in front of the head, you can do that using the CFrame's lookVector. Like so

local object
local Player = game.Players.LocalPlayer
local Character = Player.Character
local PlrMouse = Player:GetMouse()
local Camera = game.Workspace.CurrentCamera

local run = game:GetService("RunService")

run:BindToRenderStep("Before camera", Enum.RenderPriority.Camera.Value - 1, function(delta)
	if script.Parent.Grabbing.Value then
	    object = script.Parent.Obj.Value
	    object.CanCollide = false
	    object.Anchored = true
	    rot = Character.Head.CFrame + Character.Head.CFrame.lookVector * 4
	    object.CFrame = rot
	end
end)

We use bind to render step and set the priority to be higher than the camera's. Then roblox is going to execute our code first, so the movement happens before the frame is rendered, removing jittering.

8 comments

0

Oh, yes! Thank you very much! How would I interpolate it? Any wiki links?

1

http://wiki.roblox.com/index.php?title=CFrame#Methods the lerp method. However as you are already executing as fast as you can some modification should be done.

1

Also rather than interpolating I think it would be best to average the movement. Let me try it and report back

0

Ok, I'll wait. Thanks for all the help!

1

Fixed, the trick is to do the changes before roblox renders the frame

0

So how would I do that?

1

Look at the new code haha

0

Oh wow, that's beautiful. Thank you so much man! I appreciate it!

local object
local Player = game.Players.LocalPlayer
local Character = Player.Character
local PlrMouse = Player:GetMouse()
local Camera = game.Workspace.CurrentCamera

while true do
    wait()
    while script.Parent.Grabbing.Value == true do
        object = script.Parent.Obj.Value
        object.CanCollide = false
        wait()
        rot = Vector3.new(PlrMouse.X, PlrMouse.Y, PlrMouse.Z)
        object.Position = rot
    end
end

--Try origin not hit. Tell me if it works.-- Try the new code

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