TrackMouse

Overview

TrackMouse package allows you to track mouse position and use that position in your game.

trackmouse
(frame rate on this recording is really bad, but normally there is no problem, this is just bad gif)

Package consists of following items:
TM_PlayerScript - script with main logic, should be on player
TM_RedDotWidget - widget to debug where pointer is currently
TrackMouseForPlayer - script folder with both other items added and preconfigured

Usage

  • Drop folder TrackMouseForPlayer on your player template
  • Configure player template to have Orbit camera
  • Make sure that camera angle is NOT locked

Run preview and see red dot on widget moving - that is your mouse pointer.
Now on client you can read the position of mouse in another script like this (for example):

function MyScript:LocalOnTick()
	local player = self:GetEntity()
	Printf("{1}", player.TM_PlayerScript.mouseX)
	Printf("{1}", player.TM_PlayerScript.mouseY)
end

Position is not automatically sent to server, because that might take too much resources. You need to send position to server only when it is needed for your game (f.e. when you need to shoot at this point after player pressed shoot button, that kinda thing).

Note that your player view is locked with certain angle, this is required to make tracking work, but it is not done with checkbox in player, since the change in camera angle is used to define mouse movement diff.

Configuration

Properties on TM_PlayerScript allow to somewhat change configs:

  • cameraYaw, cameraPitch - you can adjust under which angle camera is locked, but there are known bugs when setting yaw close to 180. Actual value 180 should work fine because of workaround I made, but values close to it (like 179) - might break mouse tracking a bit. Another problem is connected to pitch - if camera is too high ( values close to -90 ) - movement in vertical direction would be either blocked completely or much slower. Currently calibration is made only for value of pitch “-75”. Other values might require adjusting some currently hardcoded values. Positive pitch would probably not work at all.
  • redDotEnabled - disable communicating mouse position to TM_RedDotWidget. You probably do not need this enabled, as it is only intended to debug. You might want to hide the widget as well if this is disabled.
  • pointerUpperBound, pointerLowerBound - defines how far you allow mouse to move outside of view , currently they work for both X and Y without distinction (f.e. lower bound for X is same as lower bound for Y)