From d52cded64eff4800d888c2d3c0afc262826ab999 Mon Sep 17 00:00:00 2001 From: Jens Pitkanen Date: Sun, 2 Aug 2020 03:52:38 +0300 Subject: [PATCH] Add lighting and player movement --- Assets/RenderPipeline.meta | 8 + .../DefaultLightingSettings.lighting | 63 ++++++ .../DefaultLightingSettings.lighting.meta | 8 + .../UniversalRenderPipelineAsset.asset | 55 +++++ .../UniversalRenderPipelineAsset.asset.meta | 8 + ...niversalRenderPipelineAsset_Renderer.asset | 37 ++++ ...salRenderPipelineAsset_Renderer.asset.meta | 8 + Assets/ScriptableObjects.meta | 8 + Assets/ScriptableObjects/MoveStyles.meta | 8 + .../MoveStyles/Crouching.asset | 20 ++ .../MoveStyles/Crouching.asset.meta | 8 + .../MoveStyles/Running.asset | 20 ++ .../MoveStyles/Running.asset.meta | 8 + Assets/Scripts/Options.cs | 19 ++ Assets/Scripts/Options.cs.meta | 11 + Assets/Scripts/Player.meta | 8 + Assets/Scripts/Player/LocalPlayer.cs | 65 ++++++ Assets/Scripts/Player/LocalPlayer.cs.meta | 11 + Assets/Scripts/Player/MoveStyle.cs | 20 ++ Assets/Scripts/Player/MoveStyle.cs.meta | 11 + Assets/Scripts/Player/Player.cs | 122 +++++++++++ Assets/Scripts/Player/Player.cs.meta | 15 ++ ProjectSettings/InputManager.asset | 192 ++++++++++++++++++ .../com.unity.probuilder/Settings.json | 5 + ProjectSettings/TagManager.asset | 4 +- 25 files changed, 740 insertions(+), 2 deletions(-) create mode 100644 Assets/RenderPipeline.meta create mode 100644 Assets/RenderPipeline/DefaultLightingSettings.lighting create mode 100644 Assets/RenderPipeline/DefaultLightingSettings.lighting.meta create mode 100644 Assets/RenderPipeline/UniversalRenderPipelineAsset.asset create mode 100644 Assets/RenderPipeline/UniversalRenderPipelineAsset.asset.meta create mode 100644 Assets/RenderPipeline/UniversalRenderPipelineAsset_Renderer.asset create mode 100644 Assets/RenderPipeline/UniversalRenderPipelineAsset_Renderer.asset.meta create mode 100644 Assets/ScriptableObjects.meta create mode 100644 Assets/ScriptableObjects/MoveStyles.meta create mode 100644 Assets/ScriptableObjects/MoveStyles/Crouching.asset create mode 100644 Assets/ScriptableObjects/MoveStyles/Crouching.asset.meta create mode 100644 Assets/ScriptableObjects/MoveStyles/Running.asset create mode 100644 Assets/ScriptableObjects/MoveStyles/Running.asset.meta create mode 100644 Assets/Scripts/Options.cs create mode 100644 Assets/Scripts/Options.cs.meta create mode 100644 Assets/Scripts/Player.meta create mode 100644 Assets/Scripts/Player/LocalPlayer.cs create mode 100644 Assets/Scripts/Player/LocalPlayer.cs.meta create mode 100644 Assets/Scripts/Player/MoveStyle.cs create mode 100644 Assets/Scripts/Player/MoveStyle.cs.meta create mode 100644 Assets/Scripts/Player/Player.cs create mode 100644 Assets/Scripts/Player/Player.cs.meta diff --git a/Assets/RenderPipeline.meta b/Assets/RenderPipeline.meta new file mode 100644 index 0000000..9625b58 --- /dev/null +++ b/Assets/RenderPipeline.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 816a0d14526c1de4d969eba2857628d8 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/RenderPipeline/DefaultLightingSettings.lighting b/Assets/RenderPipeline/DefaultLightingSettings.lighting new file mode 100644 index 0000000..c7a5152 --- /dev/null +++ b/Assets/RenderPipeline/DefaultLightingSettings.lighting @@ -0,0 +1,63 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!850595691 &4890085278179872738 +LightingSettings: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: DefaultLightingSettings + serializedVersion: 2 + m_GIWorkflowMode: 1 + m_EnableBakedLightmaps: 1 + m_EnableRealtimeLightmaps: 0 + m_RealtimeEnvironmentLighting: 1 + m_BounceScale: 1 + m_AlbedoBoost: 1 + m_IndirectOutputScale: 1 + m_UsingShadowmask: 1 + m_BakeBackend: 1 + m_LightmapMaxSize: 1024 + m_BakeResolution: 40 + m_Padding: 2 + m_TextureCompression: 1 + m_AO: 0 + m_AOMaxDistance: 1 + m_CompAOExponent: 1 + m_CompAOExponentDirect: 0 + m_ExtractAO: 0 + m_MixedBakeMode: 2 + m_LightmapsBakeMode: 1 + m_FilterMode: 1 + m_LightmapParameters: {fileID: 0} + m_ExportTrainingData: 0 + m_TrainingDataDestination: TrainingData + m_RealtimeResolution: 2 + m_ForceWhiteAlbedo: 0 + m_ForceUpdates: 0 + m_FinalGather: 0 + m_FinalGatherRayCount: 256 + m_FinalGatherFiltering: 1 + m_PVRCulling: 1 + m_PVRSampling: 1 + m_PVRDirectSampleCount: 32 + m_PVRSampleCount: 512 + m_PVREnvironmentSampleCount: 256 + m_PVREnvironmentReferencePointCount: 2048 + m_LightProbeSampleCountMultiplier: 4 + m_PVRBounces: 2 + m_PVRRussianRouletteStartBounce: 2 + m_PVREnvironmentMIS: 1 + m_PVRFilteringMode: 1 + m_PVRDenoiserTypeDirect: 1 + m_PVRDenoiserTypeIndirect: 1 + m_PVRDenoiserTypeAO: 1 + m_PVRFilterTypeDirect: 0 + m_PVRFilterTypeIndirect: 0 + m_PVRFilterTypeAO: 0 + m_PVRFilteringGaussRadiusDirect: 1 + m_PVRFilteringGaussRadiusIndirect: 5 + m_PVRFilteringGaussRadiusAO: 2 + m_PVRFilteringAtrousPositionSigmaDirect: 0.5 + m_PVRFilteringAtrousPositionSigmaIndirect: 2 + m_PVRFilteringAtrousPositionSigmaAO: 1 diff --git a/Assets/RenderPipeline/DefaultLightingSettings.lighting.meta b/Assets/RenderPipeline/DefaultLightingSettings.lighting.meta new file mode 100644 index 0000000..479be45 --- /dev/null +++ b/Assets/RenderPipeline/DefaultLightingSettings.lighting.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: eab3e4c589c0a1147a910511c80f5e0c +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/RenderPipeline/UniversalRenderPipelineAsset.asset b/Assets/RenderPipeline/UniversalRenderPipelineAsset.asset new file mode 100644 index 0000000..3763216 --- /dev/null +++ b/Assets/RenderPipeline/UniversalRenderPipelineAsset.asset @@ -0,0 +1,55 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: bf2edee5c58d82540a51f03df9d42094, type: 3} + m_Name: UniversalRenderPipelineAsset + m_EditorClassIdentifier: + k_AssetVersion: 5 + k_AssetPreviousVersion: 5 + m_RendererType: 1 + m_RendererData: {fileID: 0} + m_RendererDataList: + - {fileID: 11400000, guid: 329556371b5b38d4b97bf007b4a0cfcb, type: 2} + m_DefaultRendererIndex: 0 + m_RequireDepthTexture: 0 + m_RequireOpaqueTexture: 0 + m_OpaqueDownsampling: 1 + m_SupportsTerrainHoles: 1 + m_SupportsHDR: 1 + m_MSAA: 4 + m_RenderScale: 1 + m_MainLightRenderingMode: 1 + m_MainLightShadowsSupported: 1 + m_MainLightShadowmapResolution: 2048 + m_AdditionalLightsRenderingMode: 1 + m_AdditionalLightsPerObjectLimit: 4 + m_AdditionalLightShadowsSupported: 0 + m_AdditionalLightsShadowmapResolution: 512 + m_ShadowDistance: 50 + m_ShadowCascades: 0 + m_Cascade2Split: 0.25 + m_Cascade4Split: {x: 0.067, y: 0.2, z: 0.467} + m_ShadowDepthBias: 1 + m_ShadowNormalBias: 1 + m_SoftShadowsSupported: 1 + m_UseSRPBatcher: 1 + m_SupportsDynamicBatching: 0 + m_MixedLightingSupported: 1 + m_DebugLevel: 0 + m_UseAdaptivePerformance: 1 + m_ColorGradingMode: 1 + m_ColorGradingLutSize: 32 + m_ShadowType: 1 + m_LocalShadowsSupported: 0 + m_LocalShadowsAtlasResolution: 256 + m_MaxPixelLights: 0 + m_ShadowAtlasResolution: 256 + m_ShaderVariantLogLevel: 0 diff --git a/Assets/RenderPipeline/UniversalRenderPipelineAsset.asset.meta b/Assets/RenderPipeline/UniversalRenderPipelineAsset.asset.meta new file mode 100644 index 0000000..f46cba2 --- /dev/null +++ b/Assets/RenderPipeline/UniversalRenderPipelineAsset.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 714d5817425984e47bcc98849464c172 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/RenderPipeline/UniversalRenderPipelineAsset_Renderer.asset b/Assets/RenderPipeline/UniversalRenderPipelineAsset_Renderer.asset new file mode 100644 index 0000000..9bcfe4d --- /dev/null +++ b/Assets/RenderPipeline/UniversalRenderPipelineAsset_Renderer.asset @@ -0,0 +1,37 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: de640fe3d0db1804a85f9fc8f5cadab6, type: 3} + m_Name: UniversalRenderPipelineAsset_Renderer + m_EditorClassIdentifier: + m_RendererFeatures: [] + m_RendererFeatureMap: + postProcessData: {fileID: 11400000, guid: 41439944d30ece34e96484bdb6645b55, type: 2} + shaders: + blitPS: {fileID: 4800000, guid: c17132b1f77d20942aa75f8429c0f8bc, type: 3} + copyDepthPS: {fileID: 4800000, guid: d6dae50ee9e1bfa4db75f19f99355220, type: 3} + screenSpaceShadowPS: {fileID: 4800000, guid: 0f854b35a0cf61a429bd5dcfea30eddd, type: 3} + samplingPS: {fileID: 4800000, guid: 04c410c9937594faa893a11dceb85f7e, type: 3} + fallbackErrorPS: {fileID: 4800000, guid: e6e9a19c3678ded42a3bc431ebef7dbd, type: 3} + m_OpaqueLayerMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_TransparentLayerMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_DefaultStencilState: + overrideStencilState: 0 + stencilReference: 0 + stencilCompareFunction: 8 + passOperation: 0 + failOperation: 0 + zFailOperation: 0 + m_ShadowTransparentReceive: 1 diff --git a/Assets/RenderPipeline/UniversalRenderPipelineAsset_Renderer.asset.meta b/Assets/RenderPipeline/UniversalRenderPipelineAsset_Renderer.asset.meta new file mode 100644 index 0000000..cf1d019 --- /dev/null +++ b/Assets/RenderPipeline/UniversalRenderPipelineAsset_Renderer.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 329556371b5b38d4b97bf007b4a0cfcb +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ScriptableObjects.meta b/Assets/ScriptableObjects.meta new file mode 100644 index 0000000..9102701 --- /dev/null +++ b/Assets/ScriptableObjects.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6e5509da920e35841a6845fa3d020421 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ScriptableObjects/MoveStyles.meta b/Assets/ScriptableObjects/MoveStyles.meta new file mode 100644 index 0000000..ccdcd22 --- /dev/null +++ b/Assets/ScriptableObjects/MoveStyles.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 74af483c66a70cf49aa4d45415d2cfcd +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ScriptableObjects/MoveStyles/Crouching.asset b/Assets/ScriptableObjects/MoveStyles/Crouching.asset new file mode 100644 index 0000000..2f7efda --- /dev/null +++ b/Assets/ScriptableObjects/MoveStyles/Crouching.asset @@ -0,0 +1,20 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c7827ce96d9ff12458f0de236abbab9b, type: 3} + m_Name: Crouching + m_EditorClassIdentifier: + TargetVelocity: 4 + StopVelocity: 3 + Acceleration: 20 + AirAcceleration: 15 + Friction: 20 + AirFriction: 15 diff --git a/Assets/ScriptableObjects/MoveStyles/Crouching.asset.meta b/Assets/ScriptableObjects/MoveStyles/Crouching.asset.meta new file mode 100644 index 0000000..941e718 --- /dev/null +++ b/Assets/ScriptableObjects/MoveStyles/Crouching.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2d9739d3745581a41ae0ed6a569766d5 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ScriptableObjects/MoveStyles/Running.asset b/Assets/ScriptableObjects/MoveStyles/Running.asset new file mode 100644 index 0000000..6b50562 --- /dev/null +++ b/Assets/ScriptableObjects/MoveStyles/Running.asset @@ -0,0 +1,20 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c7827ce96d9ff12458f0de236abbab9b, type: 3} + m_Name: Running + m_EditorClassIdentifier: + TargetVelocity: 10 + StopVelocity: 3 + Acceleration: 10 + AirAcceleration: 5 + Friction: 8 + AirFriction: 4 diff --git a/Assets/ScriptableObjects/MoveStyles/Running.asset.meta b/Assets/ScriptableObjects/MoveStyles/Running.asset.meta new file mode 100644 index 0000000..464acb2 --- /dev/null +++ b/Assets/ScriptableObjects/MoveStyles/Running.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 550df8ac62d76a64a8c621ea6b32398f +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Options.cs b/Assets/Scripts/Options.cs new file mode 100644 index 0000000..ab954aa --- /dev/null +++ b/Assets/Scripts/Options.cs @@ -0,0 +1,19 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace NeonTea.Quakeball { + /// Container for user-selected options. + public class OptionsData { + public bool InvertVerticalLook = false; + public float LookSensitivity = 0.15f; + } + + public class Options : MonoBehaviour { + private static OptionsData Singleton = new OptionsData(); + + public static OptionsData Get() { + return Singleton; + } + } +} diff --git a/Assets/Scripts/Options.cs.meta b/Assets/Scripts/Options.cs.meta new file mode 100644 index 0000000..01fa80c --- /dev/null +++ b/Assets/Scripts/Options.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0ca0b306afdebee4a9258b12c4ca457b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Player.meta b/Assets/Scripts/Player.meta new file mode 100644 index 0000000..7c838f6 --- /dev/null +++ b/Assets/Scripts/Player.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 547a2f92b7463b5479ca4bf370d32fa6 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Player/LocalPlayer.cs b/Assets/Scripts/Player/LocalPlayer.cs new file mode 100644 index 0000000..0c8b69a --- /dev/null +++ b/Assets/Scripts/Player/LocalPlayer.cs @@ -0,0 +1,65 @@ +using UnityEngine; +using UnityEngine.InputSystem; + +namespace NeonTea.Quakeball.Player { + /// A controller class for a local player. Handles input and updates the relevant components. + [RequireComponent(typeof(Player))] + public class LocalPlayer : MonoBehaviour { + public Transform Camera; + + private float CameraPitch = 0; + + private Player Player; + private InputAction LookAction; + private InputAction MoveAction; + private InputAction CrouchAction; + + private void Awake() { + Player = GetComponent(); + + CrouchAction = new InputAction("crouch", binding: "/leftCtrl"); + CrouchAction.Enable(); + + LookAction = new InputAction("look", binding: "/leftStick"); + LookAction.AddBinding("/delta"); + LookAction.Enable(); + + MoveAction = new InputAction("move", binding: "/rightStick"); + MoveAction.AddCompositeBinding("Dpad") + .With("Up", "/w") + .With("Down", "/s") + .With("Left", "/a") + .With("Right", "/d"); + MoveAction.Enable(); + + Cursor.lockState = CursorLockMode.Locked; + Cursor.visible = false; + } + + private void Update() { + OptionsData Opts = Options.Get(); + Vector2 LookInput = LookAction.ReadValue() * Opts.LookSensitivity; + Vector3 CamEulers = Camera.localEulerAngles; + CamEulers.y += LookInput.x; + CameraPitch = Mathf.Clamp(CameraPitch - LookInput.y * (Opts.InvertVerticalLook ? -1 : 1), -90, 90); + CamEulers.x = CameraPitch; + Camera.localEulerAngles = CamEulers; + Player.HeadRotation = Camera.localRotation; + } + + /// + /// The player only moves on FixedUpdates, so movement-related inputs are handled here, so they get updated right before. + /// (Script execution order should ensure that LocalPlayer is run before Player.) + /// + private void FixedUpdate() { + Vector2 MovementInput = MoveAction.ReadValue(); + Vector3 Move = new Vector3(MovementInput.x, 0, MovementInput.y); + Vector3 Eulers = Player.HeadRotation.eulerAngles; + Eulers.z = 0; + Eulers.x = 0; + Move = Quaternion.Euler(Eulers) * Move; + Player.MoveDirection = Move; + Player.MoveStyle = CrouchAction.ReadValue() ? Player.CrouchingMoveStyle : Player.RunningMoveStyle; + } + } +} diff --git a/Assets/Scripts/Player/LocalPlayer.cs.meta b/Assets/Scripts/Player/LocalPlayer.cs.meta new file mode 100644 index 0000000..ee3350c --- /dev/null +++ b/Assets/Scripts/Player/LocalPlayer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f6e08271c272323469510daa22c35599 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: -20 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Player/MoveStyle.cs b/Assets/Scripts/Player/MoveStyle.cs new file mode 100644 index 0000000..731e625 --- /dev/null +++ b/Assets/Scripts/Player/MoveStyle.cs @@ -0,0 +1,20 @@ +using UnityEngine; + +namespace NeonTea.Quakeball.Player { + /// Different modes of transportation. + [CreateAssetMenu(fileName = "Moving", menuName = "Quakeball/MoveStyle")] + public class MoveStyle : ScriptableObject { + /// The target velocity to accelerate towards. + public float TargetVelocity; + /// When decelerating, the speed at which the deceleration no longer decelerates. + public float StopVelocity; + /// The acceleration coefficient. Higher values mean faster acceleration. + public float Acceleration; + /// The acceleration coefficient in air. Higher values mean faster acceleration. + public float AirAcceleration; + /// The friction coefficient. Higher values mean faster slowdown, should be less than or equal to Acceleration. + public float Friction; + /// The friction coefficient in air. Higher values mean faster slowdown, should be less than or equal to Acceleration. + public float AirFriction; + } +} diff --git a/Assets/Scripts/Player/MoveStyle.cs.meta b/Assets/Scripts/Player/MoveStyle.cs.meta new file mode 100644 index 0000000..3b06d7b --- /dev/null +++ b/Assets/Scripts/Player/MoveStyle.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c7827ce96d9ff12458f0de236abbab9b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Player/Player.cs b/Assets/Scripts/Player/Player.cs new file mode 100644 index 0000000..7142999 --- /dev/null +++ b/Assets/Scripts/Player/Player.cs @@ -0,0 +1,122 @@ +using UnityEngine; + +namespace NeonTea.Quakeball.Player { + /// The central glue class for players (both local and remote). + /// Other classes will handle netcode/inputs, and then speak to this class in order to make the little people run around. + [RequireComponent(typeof(CharacterController))] + public class Player : MonoBehaviour { + + /// How long after running off a cliff should the player be considered "on ground"? + public float CoyoteTime; + + public MoveStyle RunningMoveStyle; + public MoveStyle CrouchingMoveStyle; + public Transform Head; + + [Header("Player head status")] + /// The euler angle of the player's head. + public Quaternion HeadRotation; + + [Header("Player movement status")] + /// The direction the player is going. + public Vector3 MoveDirection; + + /// The player's desire to jump currently. + public bool Jumping; + + /// The amount of movement the player wants to happen. + /// Without analog controls, always 0 or 1. + public float InputSpeed; + + /// The way the player is moving. + public MoveStyle MoveStyle; + + [Header("Runtime computed values")] + /// The speed at which the player is currently moving across the ground. + public Vector3 GroundVelocity; + + /// The speed at which the player is rising or falling. + public Vector3 GravitationalVelocity; + + /// The timestamp of when the player was last on the ground. + public float GroundedTime; + + [Header("Misc. technical knobs")] + public float GroundCastLength = 0.2f; + public LayerMask GroundLayer; + + [Header("Debug settings")] + public bool ShowGroundCast; + + private CharacterController CharacterController; + private Vector3 FeetPosition; + + /// The normal of the ground below the player. If there is no ground, it's Vector3.up by default. + public Vector3 GroundCast() { + RaycastHit Hit; + if (ShowGroundCast) { + Debug.DrawLine(FeetPosition, FeetPosition - Vector3.up, Color.red, 1f); + } + if (Physics.Raycast(FeetPosition, -Vector3.up, out Hit, GroundCastLength, GroundLayer)) { + return Hit.normal; + } else { + return Vector3.up; + } + } + + public bool IsGrounded() { + return Time.fixedTime - GroundedTime <= CoyoteTime; + } + + /// Sets Heading via euler angles. + public void SetRotation(float xAngle, float yAngle) { + MoveDirection = Quaternion.Euler(xAngle, yAngle, 0) * Vector3.forward; + } + + private void Awake() { + CharacterController = GetComponent(); + FeetPosition = transform.position + CharacterController.center - Vector3.up * (CharacterController.height / 2 + CharacterController.skinWidth / 2); + } + + private void Update() { + Head.localRotation = Quaternion.Lerp(Head.localRotation, HeadRotation, 10f * Time.deltaTime); + } + + private void FixedUpdate() { + bool Grounded = IsGrounded(); + + if (Grounded) { + GravitationalVelocity = Vector3.zero; + } else { + GravitationalVelocity += Physics.gravity * Time.fixedDeltaTime; + } + + float FrictionVelocityFactor = Mathf.Max(GroundVelocity.magnitude, MoveStyle.StopVelocity); + float Deccel = FrictionVelocityFactor * Time.fixedDeltaTime; + if (Grounded) { + Deccel *= MoveStyle.Friction; + } else { + Deccel *= MoveStyle.AirFriction; + } + float FrictionedVelocity = Mathf.Max(0, GroundVelocity.magnitude - Deccel); + GroundVelocity = GroundVelocity.normalized * FrictionedVelocity; + + Vector3 GroundNormal = GroundCast(); + Vector3 FixedHeading = Vector3.ProjectOnPlane(MoveDirection, GroundNormal).normalized; + float CurrentSpeed = Vector3.Dot(GroundVelocity, FixedHeading); + float Acceleration = MoveStyle.TargetVelocity * Time.fixedDeltaTime; + if (Grounded) { + Acceleration *= MoveStyle.Acceleration; + } else { + Acceleration *= MoveStyle.AirAcceleration; + } + Acceleration = Mathf.Min(Acceleration, MoveStyle.TargetVelocity - CurrentSpeed); + GroundVelocity += FixedHeading * Acceleration; + + CharacterController.Move((GroundVelocity + GravitationalVelocity) * Time.fixedDeltaTime); + if (CharacterController.isGrounded) { + GroundedTime = Time.fixedTime; + } + } + } +} diff --git a/Assets/Scripts/Player/Player.cs.meta b/Assets/Scripts/Player/Player.cs.meta new file mode 100644 index 0000000..98ee946 --- /dev/null +++ b/Assets/Scripts/Player/Player.cs.meta @@ -0,0 +1,15 @@ +fileFormatVersion: 2 +guid: 40221663adaa8b54cb9b9f81042bd6b3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: + - RunningMoveStyle: {fileID: 11400000, guid: 550df8ac62d76a64a8c621ea6b32398f, type: 2} + - CrouchingMoveStyle: {fileID: 11400000, guid: 2d9739d3745581a41ae0ed6a569766d5, type: 2} + - Head: {instanceID: 0} + - MoveStyle: {instanceID: 0} + executionOrder: 5 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ProjectSettings/InputManager.asset b/ProjectSettings/InputManager.asset index 17c8f53..b16147e 100644 --- a/ProjectSettings/InputManager.asset +++ b/ProjectSettings/InputManager.asset @@ -293,3 +293,195 @@ InputManager: type: 0 axis: 0 joyNum: 0 + - serializedVersion: 3 + m_Name: Enable Debug Button 1 + descriptiveName: + descriptiveNegativeName: + negativeButton: + positiveButton: left ctrl + altNegativeButton: + altPositiveButton: joystick button 8 + gravity: 0 + dead: 0 + sensitivity: 0 + snap: 0 + invert: 0 + type: 0 + axis: 0 + joyNum: 0 + - serializedVersion: 3 + m_Name: Enable Debug Button 2 + descriptiveName: + descriptiveNegativeName: + negativeButton: + positiveButton: backspace + altNegativeButton: + altPositiveButton: joystick button 9 + gravity: 0 + dead: 0 + sensitivity: 0 + snap: 0 + invert: 0 + type: 0 + axis: 0 + joyNum: 0 + - serializedVersion: 3 + m_Name: Debug Reset + descriptiveName: + descriptiveNegativeName: + negativeButton: + positiveButton: left alt + altNegativeButton: + altPositiveButton: joystick button 1 + gravity: 0 + dead: 0 + sensitivity: 0 + snap: 0 + invert: 0 + type: 0 + axis: 0 + joyNum: 0 + - serializedVersion: 3 + m_Name: Debug Next + descriptiveName: + descriptiveNegativeName: + negativeButton: + positiveButton: page down + altNegativeButton: + altPositiveButton: joystick button 5 + gravity: 0 + dead: 0 + sensitivity: 0 + snap: 0 + invert: 0 + type: 0 + axis: 0 + joyNum: 0 + - serializedVersion: 3 + m_Name: Debug Previous + descriptiveName: + descriptiveNegativeName: + negativeButton: + positiveButton: page up + altNegativeButton: + altPositiveButton: joystick button 4 + gravity: 0 + dead: 0 + sensitivity: 0 + snap: 0 + invert: 0 + type: 0 + axis: 0 + joyNum: 0 + - serializedVersion: 3 + m_Name: Debug Validate + descriptiveName: + descriptiveNegativeName: + negativeButton: + positiveButton: return + altNegativeButton: + altPositiveButton: joystick button 0 + gravity: 0 + dead: 0 + sensitivity: 0 + snap: 0 + invert: 0 + type: 0 + axis: 0 + joyNum: 0 + - serializedVersion: 3 + m_Name: Debug Persistent + descriptiveName: + descriptiveNegativeName: + negativeButton: + positiveButton: right shift + altNegativeButton: + altPositiveButton: joystick button 2 + gravity: 0 + dead: 0 + sensitivity: 0 + snap: 0 + invert: 0 + type: 0 + axis: 0 + joyNum: 0 + - serializedVersion: 3 + m_Name: Debug Multiplier + descriptiveName: + descriptiveNegativeName: + negativeButton: + positiveButton: left shift + altNegativeButton: + altPositiveButton: joystick button 3 + gravity: 0 + dead: 0 + sensitivity: 0 + snap: 0 + invert: 0 + type: 0 + axis: 0 + joyNum: 0 + - serializedVersion: 3 + m_Name: Debug Horizontal + descriptiveName: + descriptiveNegativeName: + negativeButton: left + positiveButton: right + altNegativeButton: + altPositiveButton: + gravity: 1000 + dead: 0.001 + sensitivity: 1000 + snap: 0 + invert: 0 + type: 0 + axis: 0 + joyNum: 0 + - serializedVersion: 3 + m_Name: Debug Vertical + descriptiveName: + descriptiveNegativeName: + negativeButton: down + positiveButton: up + altNegativeButton: + altPositiveButton: + gravity: 1000 + dead: 0.001 + sensitivity: 1000 + snap: 0 + invert: 0 + type: 0 + axis: 0 + joyNum: 0 + - serializedVersion: 3 + m_Name: Debug Vertical + descriptiveName: + descriptiveNegativeName: + negativeButton: down + positiveButton: up + altNegativeButton: + altPositiveButton: + gravity: 1000 + dead: 0.001 + sensitivity: 1000 + snap: 0 + invert: 0 + type: 2 + axis: 6 + joyNum: 0 + - serializedVersion: 3 + m_Name: Debug Horizontal + descriptiveName: + descriptiveNegativeName: + negativeButton: left + positiveButton: right + altNegativeButton: + altPositiveButton: + gravity: 1000 + dead: 0.001 + sensitivity: 1000 + snap: 0 + invert: 0 + type: 2 + axis: 5 + joyNum: 0 diff --git a/ProjectSettings/Packages/com.unity.probuilder/Settings.json b/ProjectSettings/Packages/com.unity.probuilder/Settings.json index 65aa8ae..6a92adb 100644 --- a/ProjectSettings/Packages/com.unity.probuilder/Settings.json +++ b/ProjectSettings/Packages/com.unity.probuilder/Settings.json @@ -27,6 +27,11 @@ "type": "UnityEngine.ProBuilder.SemVer, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "key": "preferences.version", "value": "{\"m_Value\":{\"m_Major\":4,\"m_Minor\":2,\"m_Patch\":3,\"m_Build\":-1,\"m_Type\":\"\",\"m_Metadata\":\"\",\"m_Date\":\"\"}}" + }, + { + "type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "lightmapping.autoUnwrapLightmapUV", + "value": "{\"m_Value\":true}" } ] } diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 1c92a78..e1753c3 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -13,8 +13,8 @@ TagManager: - UI - - - - - - + - CollidableEnvironment + - BulletHitLayer - - -