Pxtr7ck
(Patrick)
January 24, 2020, 1:59am
#1
Hello Parzival_Pizza here, ive been trying to work on a game where if the centre of the camera is looking in at any area of a part. it will do something. But seeing im aiming this game towards mobile, this doesn’t seem to work.
I have tried using the mouse.target, but that doesn’t seem to work on mobile, but it works on PC. I have also tried locking the mouse in the centre, works on laptop, but not on mobile.
Is there any way of doing this?
2 Likes
GregTame
(GregTame)
January 24, 2020, 2:25am
#2
you could try raycasting are you trying to tap on a part, or detect if it’s field of view? i’m kinda confused by your wording.
Pxtr7ck
(Patrick)
January 24, 2020, 2:31am
#3
im trying to check if something is in the near the centre of the screen(client) and if there is, something will happen
1 Like
GregTame
(GregTame)
January 24, 2020, 2:42am
#4
does the script know what these objects are? because if so you can do some simple trig to calculate angular difference between the lookVector of the camera and the positional differences of the camera and the object.
colbert2677
(ImagineerColbert)
January 24, 2020, 2:44am
#5
I spent a couple of seconds searching and found topics. Make sure you do the same in the future. Query is something along the lines of an alternative for mouse.Target.
Thread list (some are threads, some are directly to the replies)
Hey I’ve tried to use TouchTapInWorld and use the first parameter to create a ray by putting it in :ViewportPointToRay() but that keeps returning nil. This is the script that I’ve created
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local uis = game:GetService("UserInputService")
local camera = game.Workspace.CurrentCamera
local filter = {}
uis.TouchTapInWorld:Connect(function(arg1, arg2)
local ray = camera:ViewportPointToRay(arg1.X, arg1.Y , 900)
…
A misconception I’d like to clear up first: the mouse doesn’t have a CFrame. Mouse.Hit describes a point in the 3D world where the mouse effectively “hits”. The Mouse is a 2D object. That being said, GetMouseLocation returns the location of the mouse in 2D space.
If you’re looking for an equivalent of getting where the mouse is pointing to in the world, you will need to use ViewportPointToRay as well as Mouse.UnitRay . This unit ray will need to be stretched as it only has a length magnitude of …
Using mouse.Target is a bit of a hack, rather it’s canonically incorrect, and should not be used on devices without a mouse. You should instead use the Vector2 coordinates provided to you by the function TouchTapInWorld and construct a unit ray via ViewportPointToRay , then extend that forward.
Try using the Camera:ViewportPointToRay method, which ignores the offset that comes from the core gui.
The alternative to this would be hardcoding the offset in:
local unitray = camera:ScreenPointToRay(x, y + 36, 0)
That would be because you are using mouse.Hit. You are not supposed to be using mouse objects for mobile devices that don’t have a mouse!
See: ViewportPointToRay . There are many threads on the DevForum about working with this function over using mouse.Hit for certain systems. Worth a good search.
For anyone curious:
Use a event and detect if
UserInputService.InputChanged:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseMovement and not gameProcessed then
local ray = Camera:ViewportPointToRay(inputObject.Position.X, inputObject.Position.Y)
end
end)
After getting the ray, it’s just a simple matter of ray manipulation, getting Ray.hit, etc.
When you access the .Hit property, it’s actually doing a raycast operation outward from the mouse and then returning the CFrame of where the ray hit (hence the name). The .Target property is similar, but returns the object it hit.
You could actually replicate this same behavior in your code:
local userInput = game:GetService("UserInputService")
local cam = workspace.CurrentCamera
-- Get mouse position:
local mousePos = userInput:GetMouseLocation()
-- Get the ray from the mouse and project i…
It’s reasons like this why I don’t typically use Mouse.Target. For the time being, you can use Mouse.TargetFilter and set it to the terrain to ignore the click.
What I typically do is make use of raycasts, since that’s essentially what both Mouse.Target and Mouse.Hit equate to internally. I use Camera.ViewportPointToRay with the position of the input (UserInputService), extend that ray then pass as many objects as I want into the ignore table. The first return of a ray function, Hit, is what I …
I’m not quite sure what you mean when you refer to the usage of UserInputService, but you can handle this from virtually any of the input state functions (e.g. InputBegan ). Remember that an InputObject is passed while using these functions. You can thus access InputObject.Position to get the coordinates at which the input happened.
For computer, it’s the mouse location.
For mobile, it’s the location of the touch.
AFAIK for console, it’s the center of the screen.
If you need something function…
Does Enum.UserInputType.TouchTap not suffice for your use case?
It does but it’s unrecommended to use it because Mouse.Hit doesn’t update live on mobile devices and mobile devices don’t have a mouse. Consider ViewportPointToRay, which constructs a unit ray based off of a 2D position. Here, you can feed the position of the touch via InputObject.Position.
Mouse.Hit is a simplification of the above anyway, except it also extends the unit ray.
You didn’t really ask a question. What are you trying to achieve? That’s not clear to me after reading the thread a few times.
A note that if you’re trying to search for a point in 3D space, you should use ViewportPointToRay to get more accurate results, as ViewportPoint ignores the GUI inset. Furthermore, you should be using an InputObject position over GetMouseLocation. Use InputBegan over Humanoid.Running.
That aside, if you’re trying to get a Gui object where your pointer is, you’re lookin…
6 Likes
Pxtr7ck
(Patrick)
January 24, 2020, 2:49am
#6
i did research, but i dont believe the right topics
colbert2677
(ImagineerColbert)
January 24, 2020, 2:52am
#7
There’s your list now. Take a bit to read through those threads. They have alternatives to using mouse.Target that you should adopt for both mobile and computers.
You can use Camera | Documentation - Roblox Creator Hub to determine if a part is visible on screen
You can also get the width and height of the screen using Mouse. The center of the screen is Mouse.ViewSizeX/2, Mouse.ViewSizeY/2.
Compare the position of the part onscreen and the center of the screen. If the two values are close enough, count that part as being “looked at”