So I need to make a system that moves models using the players camera, I am confused about this and I will include some code samples below. Its for a mobile version on the system which needs to be like lumber tycoon 2. Here are some of my code attempts:
Current Module:
local ServicesModule = require(script.Parent.Parent.ServicesModule)
local CAS = ServicesModule["ContextActionService"]
local UserInputService = ServicesModule["UserInputService"]
local PhysicsService = ServicesModule["PhysicsService"]
local ReplicatedStorage = ServicesModule["ReplicatedStorage"]
local PizzaFollowModule = {}
local GrabObject = nil
local GrabStart = false
local Dragger = nil
local GrabModel = workspace.Pizza
local player = ServicesModule["Players"].LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local camera = game.Workspace.CurrentCamera
-- // Welding function:
local function weldBetween(a, b)
local weld = Instance.new("ManualWeld", a)
--weld.C0 = a.CFrame:inverse() * b.CFrame
weld.Part0 = a
weld.Part1 = b
return weld
end
-- // Collision Function:
local previousCollisionGroups = {}
local function setCollisionGroup(object)
if object ~= nil then
if object:IsA("BasePart") then
previousCollisionGroups[object] = object.CollisionGroupId
PhysicsService:SetPartCollisionGroup(object, "Player Local")
end
end
end
local function setCollisionGroupRecursive(object)
setCollisionGroup(object)
for _, child in ipairs(object:GetChildren()) do
setCollisionGroupRecursive(child)
end
end
local function resetCollisionGroup(object)
local previousCollisionGroupId = previousCollisionGroups[object]
if not previousCollisionGroupId then return end
local previousCollisionGroupName = PhysicsService:GetCollisionGroupName(previousCollisionGroupId)
if not previousCollisionGroupName then return end
PhysicsService:SetPartCollisionGroup(object, previousCollisionGroupName)
previousCollisionGroups[object] = nil
end
local function onCharacterAdded(character)
setCollisionGroupRecursive(character)
character.DescendantAdded:Connect(setCollisionGroup)
character.DescendantRemoving:Connect(resetCollisionGroup)
end
-- // Pizza Collision:
local function SetPizzaCollision(Model)
for i, v in pairs(Model:GetChildren()) do
if v:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(v, "Pizza Local")
end
end
end
-- // Creating the mover:
function addMover(part)
local newMover = Instance.new("BodyPosition")
newMover.Parent = part
newMover.MaxForce = Vector3.new(40000,40000,40000)
newMover.P = 15000
newMover.D = 1000
newMover.Position = part.Position
newMover.Name = "Mover"
local newRot = Instance.new("BodyGyro")
newRot.Parent = part
newRot.MaxTorque = Vector3.new(3000,3000,3000)
newRot.P = 3000
newRot.D = 500
newRot.CFrame = game.Workspace.CurrentCamera.CFrame
newRot.Name = "RotMover"
local RotOffset = Instance.new("CFrameValue")
RotOffset.Name = "RotOffset"
RotOffset.Parent = part
end
-- // Creating the dragable object
function createDragBall()
local DragBall = Instance.new("Part")
-- // DragBall.BrickColor = BrickColor.new("Electric blue")
-- // DragBall.Material = Enum.Material.Wood
DragBall.Transparency = 1
DragBall.Size = Vector3.new(.2,.2,.2)
DragBall.Shape = "Ball"
DragBall.Name = "DragBall"
DragBall.Parent = workspace
return DragBall
end
-- // Grab function:
function Grab(actionName, UserInputState, InputObject)
if actionName == "Grab" then
if UserInputState == Enum.UserInputState.Begin then
-- start grab
local Magnitude = (mouse.Hit.Position - character.Head.CFrame.Position).magnitude
if Magnitude < 10 then
if mouse.Target then
GrabObject = mouse.Target
GrabStart = true
if GrabObject.Name == "Pizza" or GrabObject.Parent.Name == "Pizza" then
print("OBJ")
-- for i, v in pairs(GrabObject.Parent:GetChildren()) do
-- v.CanCollide = false
-- end
end
local DragBall = createDragBall()
DragBall.CFrame = mouse.Hit
Dragger = DragBall
mouse.TargetFilter = GrabObject
local DragBallWeld = weldBetween(DragBall,GrabObject)
addMover(DragBall)
while Dragger do
--Create a ray from the users head to the mouse.
local cf = CFrame.new(character.Head.Position, mouse.Hit.Position)
Dragger.Mover.Position = (cf + (cf.LookVector * 6)).Position
Dragger.RotMover.CFrame = camera.CFrame * CFrame.Angles(Dragger.RotOffset.Value.X,Dragger.RotOffset.Value.Y, Dragger.RotOffset.Value.Z)
task.wait()
end
mouse.TargetFilter = nil
end
end
elseif UserInputState == Enum.UserInputState.End then
if GrabObject ~= nil then
if GrabObject.Name == "Main" then
--for i, v in pairs(GrabObject.Parent:GetChildren()) do
-- v.CanCollide = true
--end
end
end
GrabObject = nil
GrabStart = false
if Dragger then
Dragger:Destroy()
Dragger = nil
end
end
end
end
-- // Mobile grab function:
local function MobileGrab()
local PlayerGUI = player.PlayerGui
local Mouse = player:GetMouse() -- // This works on mobile
local Magnitude = (mouse.Hit.Position - character.Head.CFrame.Position).magnitude
local Button = PlayerGUI.PlaceGUI.Drop
Button.MouseButton1Down:Connect(function()
PlayerGUI.PlaceGUI.Enabled = false
GrabModel.PrimaryPart.LocalPizzaPrompt.Enabled = true
GrabObject = nil
GrabStart = false
if Dragger then
Dragger:Destroy()
Dragger = nil
end
end)
if Magnitude < 10 then
local DragBall = createDragBall()
DragBall.CFrame = mouse.Hit
Dragger = DragBall
mouse.TargetFilter = GrabObject
local DragBallWeld = weldBetween(DragBall,GrabObject)
addMover(DragBall)
local cf = CFrame.new(character.Head.Position, Vector3.new(camera.CFrame.Position.X, 5, 0))
Dragger.Mover.Position = (character.Head.Position + Vector3.new(5, 3, 0))
--Dragger.RotMover.CFrame = camera.CFrame.LookVector * CFrame.Angles(camera.CFrame.Rotation.X, camera.CFrame.Rotation.Y, camera.CFrame.Rotation.Z)
while Dragger do
local cf = CFrame.new(character.Head.Position, Vector3.new(camera.CFrame.Position.X, 5, 0))
Dragger.Mover.Position = (character.Head.Position + Vector3.new(5, 3, 0))
--Dragger.RotMover.CFrame = camera.CFrame * CFrame.Angles(Dragger.RotOffset.Value.X,Dragger.RotOffset.Value.Y, Dragger.RotOffset.Value.Z)
task.wait(0.08)
end
end
end
-- // Mobile init function:
local function Mobile_Init()
local PC, Touchscreen = (UserInputService.MouseEnabled and UserInputService.KeyboardEnabled), UserInputService.TouchEnabled
if Touchscreen then
local NewPrompt = Instance.new("ProximityPrompt")
NewPrompt.Name = "LocalPizzaPrompt"
NewPrompt.MaxActivationDistance = 10
NewPrompt.Parent = workspace.Pizza.PrimaryPart
NewPrompt.ClickablePrompt = true
NewPrompt.RequiresLineOfSight = false
NewPrompt.Triggered:Connect(function()
NewPrompt.Enabled = false
player.PlayerGui.PlaceGUI.Enabled = true
--workspace.Pizza:SetPrimaryPartCFrame(workspace.Pizza.PrimaryPart.CFrame + Vector3.new(0, 3, 0))
MobileGrab()
end)
else
CAS:BindAction("Grab", Grab, false, Enum.UserInputType.MouseButton1)
end
end
Mobile_Init()
task.wait(4)
onCharacterAdded(character)
return PizzaFollowModule
Old code attempt:
-- // Services:
local WorkSpace = game:GetService("Workspace")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
-- // Camera:
local Camera = WorkSpace.Camera
-- // Model import
local Model = nil
local player = Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local function lookAt(target, eye)
local forwardVector = (eye - target).Unit
local upVector = Vector3.new(0, 1, 0)
local rightVector = forwardVector:Cross(upVector)
local upVector2 = rightVector:Cross(forwardVector)
return CFrame.fromMatrix(eye, rightVector, upVector2)
end
-- // Run Service Loop:
RunService.RenderStepped:Connect(function()
if Model ~= nil then
local ROT = Camera.CFrame.lookVector
local TorsoPosition = Character:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera
local worldPoint = Character.HumanoidRootPart.Position
local vector, inViewport = camera:WorldToViewportPoint(worldPoint)
local XVec = vector.X
local YVec = vector.Y
local ZVec = vector.Z
-- // Caculating 10 studs infront
local saved
saved = XVec
XVec = (XVec + -10) - saved
saved = YVec
YVec = 5
Model:SetPrimaryPartCFrame(CFrame.new(Vector3.new(vector)))
--[[Model.PrimaryPart.CFrame = CFrame.new(
Vector3.new(
TorsoPosition.Position.X + 6,
TorsoPosition.Position.Y,
TorsoPosition.Position.Z
)
)--]]
end
end)
wait(10)
Model = WorkSpace.Pizza
Deeply confused, the model needs to move about 5 studs in front/behind the player opposite the oritentation of the camera, any help would be appreciated.