Wednesday 2 March 2016

PUN shooter #16 - Mouselook and Classic FPS controls

Since I started the Bad Bears quiet alpha a common request is the classic mouse-look found in many first person shooting games (apparently made standard by Quake). I decided to implement this today.

I've been trying to get nicely decoupled input:
  • Avatar objects don't contain input code. This avoids PhotonView.isMine bulls**** symptomatic of poorly designed (multiplayer) avatar controllers. 
  • Makes it easier to write non player characters based on same avatar code.
  • Makes it easier to reuse input components for something else.
Here's how it works (follow links for source code):
  • [MouseDelta] produces a smooth output vector while the mouse is moving. The mouse cursor is hidden, locked while this component is enabled. One point of note is that, while the mouse cursor is locked, Input.mousePosition will return the same value at all times; input axes "Mouse X", "Mouse Y" are used instead. Its output is consumed by a [RotationController] which rotates the avatar that the camera is attached to.
  • Likewise [Joystick2] processes "Vertical" and "Horizontal" inputs (by default, maps WASD), sending the result to a [MotionController].
The above uses [Vector2Input] and [Vector3Input] to decouple consumers from their inputs. Oh, and you need my [god class].

One potential limitation with the above is that the avatar needs to be kept upright when using a trivial first person camera setup (camera parented to the avatar). I used [KeepUpright] to avoid this problem.


No comments:

Post a Comment