How do I detect screen touched/mousedown where on all platforms?

So I am trying to make a projectile system where a player clicks when he wants to fire. When ever the player clicks, it sends the location where the mouse/click location was. For computer, I can do this with mousedown or userinputservice. The problem is I want this to work on xbox and tablet. How do I do this?

2 Likes

I believe for mobile devices, the mouse technique still works. MouseButton1Down and events like those work, so I know that you can use mouse.

Also, to detect where a click/tap is in 3D world, you can use Mouse.Hit.p to get the Vector3 position. For more information, feel free to visit the Dev Hub.

3 Likes

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 functionally identical but more powerful than Mouse.Hit(.Position), then feed the position coordinates into Camera.ViewportPointToRay and extend that ray by creating a new one.

4 Likes

For mobile support (assuming you’re making a gun system) I use a system similar to this article that uses TouchTapInWorld to fetch the Vector2 position of the TouchTap and create a ray to get the actual position in the worldspace.

For console support (since cursor is locked onto the center of the screen), I recommend using ScreenPointToRay, found here.

Hope this helped!

3 Likes