Skip to content

Instantly share code, notes, and snippets.

@Dinir
Last active August 21, 2025 00:40
Show Gist options
  • Save Dinir/38ee530ea41ad05744952a446d83c611 to your computer and use it in GitHub Desktop.
Save Dinir/38ee530ea41ad05744952a446d83c611 to your computer and use it in GitHub Desktop.
A Template to use with Input System Package in Unity Engine
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
/*
* mov: stores movement data and related logic
* dir: stores direction data and related logic
* pos: (UI Map) stores mouse/pointer position data and related logic
* vel: stores vertical velocity value for gravity
* cam: camera attached on Player Input component's Camera field
*
* Player Input component's Behavior should be set to Send Messages
*/
Vector2 mov; // WASD, Left Stick
Vector2 dir; // Mouse, Right Stick
Vector2 pos; // Pointer Position (when UI/Point map is active)
Vector3 vel; // y value for gravity
Camera cam; // Player Input's Camera, or fallbacks to Main Camera
Transform camT; // camera's transform
Transform body; // transform this script is attached to
CharacterController controller; // character controller of the gameobject
[Header("Movement")]
[SerializeField] float speed = 5f;
[SerializeField] float gravity = -9.81f;
[Header("Look")]
[SerializeField] float mouseSensitivity = 8f;
readonly float sensitivityEditorAdjustment = 3f;
float xRotation = 0f;
[Header("Interact")]
[SerializeField] float interactMaxDistance = 10f;
[SerializeField] LayerMask interactMask = ~0;
void Awake()
{
controller = GetComponent<CharacterController>();
body = transform;
PlayerInput pi = GetComponent<PlayerInput>();
cam = pi && pi.camera ? pi.camera : Camera.main;
camT = cam ? cam.transform : null;
mouseSensitivity *= Application.isPlaying ?
sensitivityEditorAdjustment : 1f;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void OnMove(InputValue v) => mov = v.Get<Vector2>();
void OnLook(InputValue v) => dir = v.Get<Vector2>();
void OnPoint(InputValue v) => pos = v.Get<Vector2>();
void OnInteract()
{
Vector2 position = (pos.sqrMagnitude > 0f) ?
pos :
(
Mouse.current != null ?
Mouse.current.position.ReadValue() :
new Vector2(Screen.width * .5f, Screen.height * .5f)
);
Ray ray = cam.ScreenPointToRay(position);
if (Physics.Raycast(
ray, out RaycastHit hit, interactMaxDistance, interactMask
))
{
hit.collider.gameObject.SendMessage(
"OnInteract",
SendMessageOptions.DontRequireReceiver
);
}
}
void Update()
{
float dT = Time.deltaTime;
// movement
if (controller)
{
Vector3 m = (body.right * mov.x + body.forward * mov.y) * (speed * dT);
if (controller.isGrounded && vel.y < 0f) vel.y = -2f;
vel.y += gravity * dT;
controller.Move(m + dT * vel);
}
// look
if (camT)
{
float dX = mouseSensitivity * dT * dir.x;
float dY = mouseSensitivity * dT * dir.y;
xRotation = Mathf.Clamp(
xRotation - dY,
-90f,
90f
); // pitch
camT.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
body.Rotate(Vector3.up * dX); // yaw
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment