Position Object infront of the Camera using LookVector

  1. What do you want to achieve? Keep it simple and clear!
  • I want to make it so whenever i pick up an object, it positions it infront of the camera but instead of that it positions it INSIDE the camera and not 1.5 studs away
  1. What is the issue? Include screenshots / videos if possible!

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
  • Yes i didn’t find any.

///

Here’s my code.

local Player = game.Players:GetPlayerFromCharacter(script.Parent)
local Character = script.Parent

local UIS = game:GetService("UserInputService")
local Mouse = Player:GetMouse()
local TweenService = game:GetService("TweenService")

local ServiceInfos = {
	FoodInfo = TweenInfo.new(0.1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
}

local Dropped = false
local Select = false
local Object = nil

Mouse.Move:Connect(function()
	pcall(function()
		if Mouse.Target:GetAttribute("Food") and Select == false then
			local GetDistance = (Character.HumanoidRootPart.Position - Mouse.Target.Position).Magnitude

			if GetDistance < 10 then
				script.Highlight.Enabled = true
				script.Highlight.Adornee = Mouse.Target
			elseif GetDistance > 10 then
				script.Highlight.Enabled = false
				script.Highlight.Adornee = nil
			end
		elseif Mouse.Target == nil or not Mouse.Target:GetAttribute("Food") then
			script.Highlight.Enabled = false
			script.Highlight.Adornee = nil
		end	
	end)	
end)

Mouse.Button1Up:Connect(function()
	if Mouse.Target:GetAttribute("Food") and Select == false then
		Object = Mouse.Target
		Select = true
		repeat wait()
			TweenService:Create(Object, ServiceInfos.FoodInfo, {CFrame = workspace.CurrentCamera.CFrame}):Play()
		until Dropped == true
		Object = nil
		Dropped = false	
	end
end)

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Q and Dropped == false then
		Dropped = true
		Select = false
	end
end)

You can use CFrame maths to achieve this. To achieve your desired result, the part that says:

TweenService:Create(Object, ServiceInfos.FoodInfo, {CFrame = workspace.CurrentCamera.CFrame}):Play()

should instead be replaced with

TweenService:Create(Object, ServiceInfos.FoodInfo, {CFrame = workspace.CurrentCamera.CFrame * CFrame.new(0, 0, -1.5)}):Play()

The general syntax is CFrame * CFrame. You can see more here.

1 Like

Yes.

I totally forgot about that, thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.