This commit is contained in:
2025-06-16 15:14:23 +02:00
committed by devbeni
parent 60fe4620ff
commit 4ff561284f
3174 changed files with 428263 additions and 0 deletions

View File

@ -0,0 +1,477 @@
using System;
using UnityEngine;
using UnityEngine.Serialization;
namespace Mirror.Examples.Common.Controllers.Player
{
[AddComponentMenu("")]
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(NetworkIdentity))]
[DisallowMultipleComponent]
public class PlayerControllerBase : NetworkBehaviour
{
const float BASE_DPI = 96f;
public enum GroundState : byte { Grounded, Jumping, Falling }
[Serializable]
public struct MoveKeys
{
public KeyCode Forward;
public KeyCode Back;
public KeyCode StrafeLeft;
public KeyCode StrafeRight;
public KeyCode TurnLeft;
public KeyCode TurnRight;
public KeyCode Jump;
}
[Serializable]
public struct OptionsKeys
{
public KeyCode MouseSteer;
public KeyCode AutoRun;
public KeyCode ToggleUI;
}
[Flags]
public enum ControlOptions : byte
{
None,
MouseSteer = 1 << 0,
AutoRun = 1 << 1,
ShowUI = 1 << 2
}
[Header("Avatar Components")]
public CharacterController characterController;
[Header("User Interface")]
public GameObject ControllerUIPrefab;
[Header("Configuration")]
[SerializeField]
public MoveKeys moveKeys = new MoveKeys
{
Forward = KeyCode.W,
Back = KeyCode.S,
StrafeLeft = KeyCode.A,
StrafeRight = KeyCode.D,
TurnLeft = KeyCode.Q,
TurnRight = KeyCode.E,
Jump = KeyCode.Space,
};
[SerializeField]
public OptionsKeys optionsKeys = new OptionsKeys
{
MouseSteer = KeyCode.M,
AutoRun = KeyCode.R,
ToggleUI = KeyCode.U
};
[Space(5)]
public ControlOptions controlOptions = ControlOptions.ShowUI;
[Header("Movement")]
[Range(0, 20)]
[FormerlySerializedAs("moveSpeedMultiplier")]
[Tooltip("Speed in meters per second")]
public float maxMoveSpeed = 8f;
// Replacement for Sensitvity from Input Settings.
[Range(0, 10f)]
[Tooltip("Sensitivity factors into accelleration")]
public float inputSensitivity = 2f;
// Replacement for Gravity from Input Settings.
[Range(0, 10f)]
[Tooltip("Gravity factors into decelleration")]
public float inputGravity = 2f;
[Header("Turning")]
[Range(0, 300f)]
[Tooltip("Max Rotation in degrees per second")]
public float maxTurnSpeed = 100f;
[Range(0, 10f)]
[FormerlySerializedAs("turnDelta")]
[Tooltip("Rotation acceleration in degrees per second squared")]
public float turnAcceleration = 3f;
[Header("Jumping")]
[Range(0, 10f)]
[Tooltip("Initial jump speed in meters per second")]
public float initialJumpSpeed = 2.5f;
[Range(0, 10f)]
[Tooltip("Maximum jump speed in meters per second")]
public float maxJumpSpeed = 3.5f;
[Range(0, 10f)]
[FormerlySerializedAs("jumpDelta")]
[Tooltip("Jump acceleration in meters per second squared")]
public float jumpAcceleration = 4f;
// Runtime data in a struct so it can be folded up in inspector
[Serializable]
public struct RuntimeData
{
[ReadOnly, SerializeField, Range(-1f, 1f)] float _horizontal;
[ReadOnly, SerializeField, Range(-1f, 1f)] float _vertical;
[ReadOnly, SerializeField, Range(-300f, 300f)] float _turnSpeed;
[ReadOnly, SerializeField, Range(-10f, 10f)] float _jumpSpeed;
[ReadOnly, SerializeField, Range(-1.5f, 1.5f)] float _animVelocity;
[ReadOnly, SerializeField, Range(-1.5f, 1.5f)] float _animRotation;
[ReadOnly, SerializeField, Range(-1f, 1f)] float _mouseInputX;
[ReadOnly, SerializeField, Range(0, 30f)] float _mouseSensitivity;
[ReadOnly, SerializeField] GroundState _groundState;
[ReadOnly, SerializeField] Vector3 _direction;
[ReadOnly, SerializeField] Vector3Int _velocity;
[ReadOnly, SerializeField] GameObject _controllerUI;
#region Properties
public float horizontal
{
get => _horizontal;
internal set => _horizontal = value;
}
public float vertical
{
get => _vertical;
internal set => _vertical = value;
}
public float turnSpeed
{
get => _turnSpeed;
internal set => _turnSpeed = value;
}
public float jumpSpeed
{
get => _jumpSpeed;
internal set => _jumpSpeed = value;
}
public float animVelocity
{
get => _animVelocity;
internal set => _animVelocity = value;
}
public float animRotation
{
get => _animRotation;
internal set => _animRotation = value;
}
public float mouseInputX
{
get => _mouseInputX;
internal set => _mouseInputX = value;
}
public float mouseSensitivity
{
get => _mouseSensitivity;
internal set => _mouseSensitivity = value;
}
public GroundState groundState
{
get => _groundState;
internal set => _groundState = value;
}
public Vector3 direction
{
get => _direction;
internal set => _direction = value;
}
public Vector3Int velocity
{
get => _velocity;
internal set => _velocity = value;
}
public GameObject controllerUI
{
get => _controllerUI;
internal set => _controllerUI = value;
}
#endregion
}
[Header("Diagnostics")]
public RuntimeData runtimeData;
#region Network Setup
protected override void OnValidate()
{
// Skip if Editor is in Play mode
if (Application.isPlaying) return;
base.OnValidate();
Reset();
}
void Reset()
{
if (characterController == null)
characterController = GetComponent<CharacterController>();
// Override CharacterController default values
characterController.enabled = false;
characterController.skinWidth = 0.02f;
characterController.minMoveDistance = 0f;
GetComponent<Rigidbody>().isKinematic = true;
#if UNITY_EDITOR
// For convenience in the examples, we use the GUID of the PlayerControllerUI
// to find the correct prefab in the Mirror/Examples/_Common/Controllers folder.
// This avoids conflicts with user-created prefabs that may have the same name
// and avoids polluting the user's project with Resources.
// This is not recommended for production code...use Resources.Load or AssetBundles instead.
if (ControllerUIPrefab == null)
{
string path = UnityEditor.AssetDatabase.GUIDToAssetPath("7beee247444994f0281dadde274cc4af");
ControllerUIPrefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(path);
}
#endif
this.enabled = false;
}
void OnDisable()
{
runtimeData.horizontal = 0f;
runtimeData.vertical = 0f;
runtimeData.turnSpeed = 0f;
}
public override void OnStartAuthority()
{
// Calculate DPI-aware sensitivity
float dpiScale = (Screen.dpi > 0) ? (Screen.dpi / BASE_DPI) : 1f;
runtimeData.mouseSensitivity = turnAcceleration * dpiScale;
SetCursor(controlOptions.HasFlag(ControlOptions.MouseSteer));
characterController.enabled = true;
this.enabled = true;
}
public override void OnStopAuthority()
{
this.enabled = false;
characterController.enabled = false;
SetCursor(false);
}
public override void OnStartLocalPlayer()
{
if (ControllerUIPrefab != null)
runtimeData.controllerUI = Instantiate(ControllerUIPrefab);
if (runtimeData.controllerUI != null)
{
if (runtimeData.controllerUI.TryGetComponent(out PlayerControllerUI canvasControlPanel))
canvasControlPanel.Refresh(moveKeys, optionsKeys);
runtimeData.controllerUI.SetActive(controlOptions.HasFlag(ControlOptions.ShowUI));
}
}
public override void OnStopLocalPlayer()
{
if (runtimeData.controllerUI != null)
Destroy(runtimeData.controllerUI);
runtimeData.controllerUI = null;
}
#endregion
void Update()
{
if (!characterController.enabled)
return;
float deltaTime = Time.deltaTime;
HandleOptions();
if (controlOptions.HasFlag(ControlOptions.MouseSteer))
HandleMouseSteer(deltaTime);
else
HandleTurning(deltaTime);
HandleJumping(deltaTime);
HandleMove(deltaTime);
ApplyMove(deltaTime);
// Reset ground state
if (characterController.isGrounded)
runtimeData.groundState = GroundState.Grounded;
else if (runtimeData.groundState != GroundState.Jumping)
runtimeData.groundState = GroundState.Falling;
// Diagnostic velocity...FloorToInt for display purposes
runtimeData.velocity = Vector3Int.FloorToInt(characterController.velocity);
}
void SetCursor(bool locked)
{
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
Cursor.visible = !locked;
}
void HandleOptions()
{
if (optionsKeys.MouseSteer != KeyCode.None && Input.GetKeyUp(optionsKeys.MouseSteer))
{
controlOptions ^= ControlOptions.MouseSteer;
SetCursor(controlOptions.HasFlag(ControlOptions.MouseSteer));
}
if (optionsKeys.AutoRun != KeyCode.None && Input.GetKeyUp(optionsKeys.AutoRun))
controlOptions ^= ControlOptions.AutoRun;
if (optionsKeys.ToggleUI != KeyCode.None && Input.GetKeyUp(optionsKeys.ToggleUI))
{
controlOptions ^= ControlOptions.ShowUI;
if (runtimeData.controllerUI != null)
runtimeData.controllerUI.SetActive(controlOptions.HasFlag(ControlOptions.ShowUI));
}
}
// Turning works while airborne...feature?
void HandleTurning(float deltaTime)
{
float targetTurnSpeed = 0f;
// TurnLeft and TurnRight cancel each other out, reducing targetTurnSpeed to zero.
if (moveKeys.TurnLeft != KeyCode.None && Input.GetKey(moveKeys.TurnLeft))
targetTurnSpeed -= maxTurnSpeed;
if (moveKeys.TurnRight != KeyCode.None && Input.GetKey(moveKeys.TurnRight))
targetTurnSpeed += maxTurnSpeed;
// If there's turn input or AutoRun is not enabled, adjust turn speed towards target
// If no turn input and AutoRun is enabled, maintain the previous turn speed
if (targetTurnSpeed != 0f || !controlOptions.HasFlag(ControlOptions.AutoRun))
runtimeData.turnSpeed = Mathf.MoveTowards(runtimeData.turnSpeed, targetTurnSpeed, turnAcceleration * maxTurnSpeed * deltaTime);
transform.Rotate(0f, runtimeData.turnSpeed * deltaTime, 0f);
}
void HandleMouseSteer(float deltaTime)
{
// Accumulate mouse input over time
runtimeData.mouseInputX += Input.GetAxisRaw("Mouse X") * runtimeData.mouseSensitivity;
// Clamp the accumulator to simulate key press behavior
runtimeData.mouseInputX = Mathf.Clamp(runtimeData.mouseInputX, -1f, 1f);
// Calculate target turn speed
float targetTurnSpeed = runtimeData.mouseInputX * maxTurnSpeed;
// Use the same acceleration logic as HandleTurning
runtimeData.turnSpeed = Mathf.MoveTowards(runtimeData.turnSpeed, targetTurnSpeed, runtimeData.mouseSensitivity * maxTurnSpeed * deltaTime);
// Apply rotation
transform.Rotate(0f, runtimeData.turnSpeed * deltaTime, 0f);
runtimeData.mouseInputX = Mathf.MoveTowards(runtimeData.mouseInputX, 0f, runtimeData.mouseSensitivity * deltaTime);
}
void HandleJumping(float deltaTime)
{
if (runtimeData.groundState != GroundState.Falling && moveKeys.Jump != KeyCode.None && Input.GetKey(moveKeys.Jump))
{
if (runtimeData.groundState != GroundState.Jumping)
{
runtimeData.groundState = GroundState.Jumping;
runtimeData.jumpSpeed = initialJumpSpeed;
}
else if (runtimeData.jumpSpeed < maxJumpSpeed)
{
// Increase jumpSpeed using a square root function for a fast start and slow finish
float jumpProgress = (runtimeData.jumpSpeed - initialJumpSpeed) / (maxJumpSpeed - initialJumpSpeed);
runtimeData.jumpSpeed += (jumpAcceleration * Mathf.Sqrt(1 - jumpProgress)) * deltaTime;
}
if (runtimeData.jumpSpeed >= maxJumpSpeed)
{
runtimeData.jumpSpeed = maxJumpSpeed;
runtimeData.groundState = GroundState.Falling;
}
}
else if (runtimeData.groundState != GroundState.Grounded)
{
runtimeData.groundState = GroundState.Falling;
runtimeData.jumpSpeed = Mathf.Min(runtimeData.jumpSpeed, maxJumpSpeed);
runtimeData.jumpSpeed += Physics.gravity.y * deltaTime;
}
else
// maintain small downward speed for when falling off ledges
runtimeData.jumpSpeed = Physics.gravity.y * deltaTime;
}
void HandleMove(float deltaTime)
{
// Initialize target movement variables
float targetMoveX = 0f;
float targetMoveZ = 0f;
// Check for WASD key presses and adjust target movement variables accordingly
if (moveKeys.Forward != KeyCode.None && Input.GetKey(moveKeys.Forward)) targetMoveZ = 1f;
if (moveKeys.Back != KeyCode.None && Input.GetKey(moveKeys.Back)) targetMoveZ = -1f;
if (moveKeys.StrafeLeft != KeyCode.None && Input.GetKey(moveKeys.StrafeLeft)) targetMoveX = -1f;
if (moveKeys.StrafeRight != KeyCode.None && Input.GetKey(moveKeys.StrafeRight)) targetMoveX = 1f;
if (targetMoveX == 0f)
{
if (!controlOptions.HasFlag(ControlOptions.AutoRun))
runtimeData.horizontal = Mathf.MoveTowards(runtimeData.horizontal, targetMoveX, inputGravity * deltaTime);
}
else
runtimeData.horizontal = Mathf.MoveTowards(runtimeData.horizontal, targetMoveX, inputSensitivity * deltaTime);
if (targetMoveZ == 0f)
{
if (!controlOptions.HasFlag(ControlOptions.AutoRun))
runtimeData.vertical = Mathf.MoveTowards(runtimeData.vertical, targetMoveZ, inputGravity * deltaTime);
}
else
runtimeData.vertical = Mathf.MoveTowards(runtimeData.vertical, targetMoveZ, inputSensitivity * deltaTime);
}
void ApplyMove(float deltaTime)
{
// Create initial direction vector without jumpSpeed (y-axis).
runtimeData.direction = new Vector3(runtimeData.horizontal, 0f, runtimeData.vertical);
// Clamp so diagonal strafing isn't a speed advantage.
runtimeData.direction = Vector3.ClampMagnitude(runtimeData.direction, 1f);
// Transforms direction from local space to world space.
runtimeData.direction = transform.TransformDirection(runtimeData.direction);
// Multiply for desired ground speed.
runtimeData.direction *= maxMoveSpeed;
// Add jumpSpeed to direction as last step.
//runtimeData.direction.y = runtimeData.jumpSpeed;
runtimeData.direction = new Vector3(runtimeData.direction.x, runtimeData.jumpSpeed, runtimeData.direction.z);
// Finally move the character.
characterController.Move(runtimeData.direction * deltaTime);
}
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 22ac0cecaad969041bfd6a297fc5f2cd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/_Common/Controllers/PlayerController/PlayerControllerBase.cs
uploadId: 736421

View File

@ -0,0 +1,8 @@
using UnityEngine;
namespace Mirror.Examples.Common.Controllers.Player
{
[AddComponentMenu("Network/Player Controller (Hybrid)")]
[RequireComponent(typeof(NetworkTransformHybrid))]
public class PlayerControllerHybrid : PlayerControllerBase { }
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 442f181dd98a15148978a961afda8f94
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/_Common/Controllers/PlayerController/PlayerControllerHybrid.cs
uploadId: 736421

View File

@ -0,0 +1,8 @@
using UnityEngine;
namespace Mirror.Examples.Common.Controllers.Player
{
[AddComponentMenu("Network/Player Controller (Reliable)")]
[RequireComponent(typeof(NetworkTransformReliable))]
public class PlayerControllerReliable : PlayerControllerBase { }
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 497221e839119e34b897d6c497cbc8e5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/_Common/Controllers/PlayerController/PlayerControllerReliable.cs
uploadId: 736421

View File

@ -0,0 +1,51 @@
using System;
using UnityEngine;
using UnityEngine.UI;
namespace Mirror.Examples.Common.Controllers.Player
{
[AddComponentMenu("")]
[DisallowMultipleComponent]
public class PlayerControllerUI : ControllerUIBase
{
[Serializable]
public struct MoveTexts
{
public Text keyTextTurnLeft;
public Text keyTextForward;
public Text keyTextTurnRight;
public Text keyTextStrafeLeft;
public Text keyTextBack;
public Text keyTextStrafeRight;
public Text keyTextJump;
}
[Serializable]
public struct OptionsTexts
{
public Text keyTextMouseSteer;
public Text keyTextAutoRun;
public Text keyTextToggleUI;
}
[SerializeField] MoveTexts moveTexts;
[SerializeField] OptionsTexts optionsTexts;
public void Refresh(PlayerControllerBase.MoveKeys moveKeys, PlayerControllerBase.OptionsKeys optionsKeys)
{
// Movement Keys
moveTexts.keyTextTurnLeft.text = GetKeyText(moveKeys.TurnLeft);
moveTexts.keyTextForward.text = GetKeyText(moveKeys.Forward);
moveTexts.keyTextTurnRight.text = GetKeyText(moveKeys.TurnRight);
moveTexts.keyTextStrafeLeft.text = GetKeyText(moveKeys.StrafeLeft);
moveTexts.keyTextBack.text = GetKeyText(moveKeys.Back);
moveTexts.keyTextStrafeRight.text = GetKeyText(moveKeys.StrafeRight);
moveTexts.keyTextJump.text = GetKeyText(moveKeys.Jump);
// Options Keys
optionsTexts.keyTextMouseSteer.text = GetKeyText(optionsKeys.MouseSteer);
optionsTexts.keyTextAutoRun.text = GetKeyText(optionsKeys.AutoRun);
optionsTexts.keyTextToggleUI.text = GetKeyText(optionsKeys.ToggleUI);
}
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 5548caf5abf51449f98ab54971eeff29
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/_Common/Controllers/PlayerController/PlayerControllerUI.cs
uploadId: 736421

View File

@ -0,0 +1,14 @@
fileFormatVersion: 2
guid: 7beee247444994f0281dadde274cc4af
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/_Common/Controllers/PlayerController/PlayerControllerUI.prefab
uploadId: 736421

View File

@ -0,0 +1,8 @@
using UnityEngine;
namespace Mirror.Examples.Common.Controllers.Player
{
[AddComponentMenu("Network/Player Controller (Unreliable)")]
[RequireComponent(typeof(NetworkTransformUnreliable))]
public class PlayerControllerUnreliable : PlayerControllerBase { }
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: f81b59082839c2e40938767457bb91ae
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/_Common/Controllers/PlayerController/PlayerControllerUnreliable.cs
uploadId: 736421