How do i can make a crosshair that will work like mouse.Target

I want to make a crosshair that works like a mouse. The goal, as well as to work on phones, I tried a lot of ways, I searched, but nothing worked, can you help me with this.

2 Likes

You want like, a custom mouse? Sorry, I don’t understand your wording too well…

Do you plan to make this First-person?

so you want the mouse fixed in the middle of the screen, having a crosshairs icon?

Oh, you want something like a custom mouse icon? If you want to print something when ‘e’ is pressed, do this;

--LocalScript
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
mouse.Icon = 'rbxassetid://crosshair_id_here' --Put  a crosshair decal id here
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(key) --Detects when the player presses a key
	if key.KeyCode == Enum.KeyCode.E then --If E was pressed
		print(mouse.Target) --Prints the mouse's target
	end
end)
1 Like

Do you plan to have the camera locked?

No I said lock not block, do you plan to have the camera locked to the way the lookVector is facing

so you want like a gui or fake ui frame mouse icon is on the mouse?

I recently made something like this, if the mouse hovers over an object, a BillboardGui will be created in the object’s position. That billboard gui also shows the object name

(my english may be not perfect)

You mean ScreenGui, not billboard gui?

Yep, this is possible

yes of course. try using render step. and just tween it if its near on a interactable object…

local Frame = Ui_Object
local Mouse = game.Players.LocalPlayer:GetMouse()
local MouseXPos = Mouse.X
local MouseYPos = Mouse.Y

game:GetService("RunService").RenderStepped:Connect(function()
    Frame:TweenPosition(UDim2.new(MouseXPos, MouseYPos), "Out", "Sine", .5, true)
    
    --note: when the mouse is hovering an object has an value or object called "InteractableObject" it will 
    --do something
    if Mouse.Target:FindFirstChild("InteractableObject") then
        --Something that u want to happen to frame
    else
        --To tween it back
        Frame:TweenPosition(UDim2.new(MouseXPos, MouseYPos), "Out", "Sine", .5, true)
    end
end)

i dont know if that would really work though… I directly typed that here.

wait im trying to do that rn…

try this?

spawn(function()
	local Frame = script.Parent.FakeMouse
	local Player = game.Players.LocalPlayer
	local Mouse = Player:GetMouse()
	
	Mouse.Icon = "rbxassetid://" -- mouse i can u want
	
	game:GetService("RunService").RenderStepped:Connect(function()
		local MouseXPos = Mouse.X
		local MouseYPos = Mouse.Y
		
		Frame.Position = UDim2.new(0, MouseXPos,0, MouseYPos)
		
	    --note: when the mouse is hovering an object has an value or object called "InteractableObject" it will 
	    --do something
	    if Mouse.Target ~= nil then
			--Something that u want to happen to frame
			if Mouse.Target:IsA("BasePart") then
				if Mouse.Target:FindFirstChild("InteractableObject") then
					--Stuff that will happen when it hover it
					print("Interact")
				end
			end
	    else
	    	--To tween it back
			Frame.Position = UDim2.new(0, MouseXPos,0, MouseYPos)
		end
	end)
end)

The crosshairs you then place on the screen via a gui.

In regard to pressing ‘E’ - this won’t work in with your current idea of having a keyboard check if you want this to work with phones, as phones don’t have keyboards. For that, you’d need to add a GUI button for the users to press, or wait for screen taps and then run your code.

-- set up
local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera
local IO	 = game:GetService 'UserInputService'
local plrGui = player:WaitForChild 'PlayerGui'
local gui	 = plrGui:WaitForChild 'ScreenGui' --> Whatever your screen gui is
local length = 500 -- how far we shoot our ray

-- private
local function getCameraFocus(ignore_list) -- Shoots a ray from the centre of the viewport frame (the camera), whilst ignoring the gui inset
	local viewportSize = camera.viewportSize
	local unitRay = camera:ScreenPointToRay(viewportSize/2, viewportSize.y/2)
	local ray = Ray.new(unitRay.Origin, unitRay.Direction * length)
	return game.Workspace:FindPartOnRayWithIgnoreList(ray, ignore_list)
end

local function isAlive(Player) -- checks if a player is alive/dead
	local char = Player.Character
	if not char or not char:IsDescendantOf(game.Workspace) or not char:FindFirstChild "Humanoid" or char.Humanoid:GetState() == Enum.HumanoidStateType.Dead then
		return
	end
	return true
end

local function create(obj) -- creates an instance with the properties passed
	obj = Instance.new(obj)
	return function (props)
		for i, v in next, props do
			obj[i] = v
		end
		return obj
	end
end

-- main
local states = {
	focus = nil; -- what object we're currently looking at
	point = nil; -- what position that object is
	frame = nil; -- the TextLabel we're using to display that info
}

game:GetService('RunService').RenderStepped:connect(function (delta)
	if isAlive(player) then
		local hit, pos, normal = getCameraFocus {player.Character}
		
		-- Do we have a target in front of our camera?
		if hit then
			-- We do, so let's find the screen point of the position we hit the target (X, Y)
			local offset = camera:WorldToScreenPoint(pos)
			-- Is this the same focus we saw last frame?
			if hit ~= states.focus then
				-- It's not, so let's check if we need to alter / create a new frame
				if states.frame and states.frame.Parent then
					-- Alter the current frame to display the new hit name + update the position
					states.frame.Text	  = hit.Name
					states.frame.Position = UDim2.new(0, offset.x, 0, offset.y)
				else
					-- Create the 'TextLabel' frame object
					states.frame = create 'TextLabel' {
						Text	 = hit.Name
						Size	 = UDim2.new(0, 100, 0, 50);
						Position = UDim2.new(0, offset.x, 0, offset.y);
						Parent 	 = gui;
					}
				end
			else
				-- Did we hit a different position? If so, let's update the frame position
				if states.frame and states.point ~= offset then
					states.frame.Position = UDim2.new(0, offset.x, 0, offset.y)
				end
			end
			states.focus = hit
			states.point = offset
		else
			-- We've not got a hit this time, so if we have an active frame, let's destroy it + reset our states
			if states.frame then
				states.frame:Destroy()
				states.focus = nil
				states.point = nil
				states.frame = nil
			end
		end
	else
		-- We're dead, so let's reset our states
		if states.frame then
			states.frame:Destroy()
			states.focus = nil
			states.point = nil
			states.frame = nil
		end
	end
end)
2 Likes