Hello,
I created a frame (the cursor) and placed it in the middle of the screen. I want a part to follow this cursor when the player moves but I can’t do it.
Thanks to anyone who could help me : )
You should read this whenever you want to know something related to a mouse.
Also here’s relevant a section of the page:
Hit
This property indicates CFrame of the mouse’s position in 3D space. Note that Mouse.TargetFilterand its descendants will be ignored.
Developers can get obtain the position of Hit like so:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local position = mouse.Hit.p -- Maybe put your part in this position.
Exactly, I would like to do exactly the same thing but with a frame that doesn’t move
My man, read what i sent you, it answers all of your questions.
Thanks but I tried to understand but I can’t seem to follow a part with this
this might be related?
Your terms are a bit confusing when you say “cursor” do you mean like a crosshair that stays in the middle of the screen?
To my understanding, you want something like this?
--[[ Local Script ]]--
local RunService = game:GetService("RunService")
local Camera = game.Workspace.CurrentCamera
local Frame = script.Parent
local part = Instance.new("Part")
part.Parent = game.Workspace
part.Anchored = true
part.Size = Vector3.new(2, 2, 2)
part.Color = Color3.new(1, 0, 0)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {part}
RunService.RenderStepped:Connect(function()
local position = Frame.AbsolutePosition
local size = Frame.AbsoluteSize
local unitRay = Camera:ScreenPointToRay(position.X + size.X/2, position.Y + size.Y/2)
local raycastResult = game.Workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, raycastParams)
local frameWorldSpace = raycastResult and raycastResult.Position or unitRay.Origin + unitRay.Direction * 1000
part.Position = frameWorldSpace
end)
This is exactly what I was looking for, thanks everyone for your help!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.