From 85f16b069755c10a959e98e4622923f4ecfdc507 Mon Sep 17 00:00:00 2001 From: DerTyp187 Date: Wed, 29 Sep 2021 18:47:21 +0200 Subject: [PATCH] Base Rigidbody Movement + FPS counter --- Assets/Scenes/SampleScene.unity | 214 +++++++++++++++++- Assets/Scripts/Character.meta | 8 + Assets/Scripts/{ => Character}/MouseLook.cs | 0 .../Scripts/{ => Character}/MouseLook.cs.meta | 0 .../{ => Character}/PlayerInteraction.cs | 0 .../{ => Character}/PlayerInteraction.cs.meta | 0 Assets/Scripts/Character/PlayerMovement.cs | 79 +++++++ .../{ => Character}/PlayerMovement.cs.meta | 0 Assets/Scripts/FPSCounter.cs | 21 ++ Assets/Scripts/FPSCounter.cs.meta | 11 + Assets/Scripts/PlayerMovement.cs | 102 --------- ProjectSettings/DynamicsManager.asset | 9 +- 12 files changed, 335 insertions(+), 109 deletions(-) create mode 100644 Assets/Scripts/Character.meta rename Assets/Scripts/{ => Character}/MouseLook.cs (100%) rename Assets/Scripts/{ => Character}/MouseLook.cs.meta (100%) rename Assets/Scripts/{ => Character}/PlayerInteraction.cs (100%) rename Assets/Scripts/{ => Character}/PlayerInteraction.cs.meta (100%) create mode 100644 Assets/Scripts/Character/PlayerMovement.cs rename Assets/Scripts/{ => Character}/PlayerMovement.cs.meta (100%) create mode 100644 Assets/Scripts/FPSCounter.cs create mode 100644 Assets/Scripts/FPSCounter.cs.meta delete mode 100644 Assets/Scripts/PlayerMovement.cs diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 861dc90..9369c8d 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -128,6 +128,36 @@ GameObject: m_CorrespondingSourceObject: {fileID: 8390537188327133021, guid: 6a86db7765f277243b912c20f451dc47, type: 3} m_PrefabInstance: {fileID: 8390537188401440993} m_PrefabAsset: {fileID: 0} +--- !u!136 &95348669 +CapsuleCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 95348668} + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_Enabled: 1 + m_Radius: 0.8 + m_Height: 3.5 + m_Direction: 1 + m_Center: {x: 0, y: 0, z: 0} +--- !u!54 &95348670 +Rigidbody: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 95348668} + serializedVersion: 2 + m_Mass: 1 + m_Drag: 0 + m_AngularDrag: 0.05 + m_UseGravity: 1 + m_IsKinematic: 0 + m_Interpolate: 0 + m_Constraints: 112 + m_CollisionDetection: 0 --- !u!114 &95348673 MonoBehaviour: m_ObjectHideFlags: 0 @@ -630,6 +660,7 @@ RectTransform: - {fileID: 1048269304} - {fileID: 1690461076} - {fileID: 114639928} + - {fileID: 1395840125} m_Father: {fileID: 0} m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -788,6 +819,155 @@ Transform: m_CorrespondingSourceObject: {fileID: 494189295979627311, guid: 613866d46c759a848b2a462973745a74, type: 3} m_PrefabInstance: {fileID: 1799643402} m_PrefabAsset: {fileID: 0} +--- !u!1 &1395840124 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1395840125} + - component: {fileID: 1395840128} + - component: {fileID: 1395840127} + - component: {fileID: 1395840126} + m_Layer: 5 + m_Name: FPSText + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &1395840125 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1395840124} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 708126143} + m_RootOrder: 4 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 1} + m_AnchorMax: {x: 0, y: 1} + m_AnchoredPosition: {x: 0, y: -3.9089966} + m_SizeDelta: {x: 78.3094, y: 46.091} + m_Pivot: {x: 0, y: 1} +--- !u!114 &1395840126 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1395840124} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1866e0fb446be0342a91f95d71fc2c2d, type: 3} + m_Name: + m_EditorClassIdentifier: + fpsText: {fileID: 0} + deltaTime: 0 +--- !u!114 &1395840127 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1395840124} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_text: 187 FPS + m_isRightToLeft: 0 + m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} + m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} + m_fontSharedMaterials: [] + m_fontMaterial: {fileID: 0} + m_fontMaterials: [] + m_fontColor32: + serializedVersion: 2 + rgba: 4278255384 + m_fontColor: {r: 0.09455109, g: 1, b: 0, a: 1} + m_enableVertexGradient: 0 + m_colorMode: 3 + m_fontColorGradient: + topLeft: {r: 1, g: 1, b: 1, a: 1} + topRight: {r: 1, g: 1, b: 1, a: 1} + bottomLeft: {r: 1, g: 1, b: 1, a: 1} + bottomRight: {r: 1, g: 1, b: 1, a: 1} + m_fontColorGradientPreset: {fileID: 0} + m_spriteAsset: {fileID: 0} + m_tintAllSprites: 0 + m_StyleSheet: {fileID: 0} + m_TextStyleHashCode: -1183493901 + m_overrideHtmlColors: 0 + m_faceColor: + serializedVersion: 2 + rgba: 4294967295 + m_fontSize: 15 + m_fontSizeBase: 15 + m_fontWeight: 400 + m_enableAutoSizing: 0 + m_fontSizeMin: 18 + m_fontSizeMax: 72 + m_fontStyle: 1 + m_HorizontalAlignment: 4 + m_VerticalAlignment: 256 + m_textAlignment: 65535 + m_characterSpacing: 0 + m_wordSpacing: 0 + m_lineSpacing: 0 + m_lineSpacingMax: 0 + m_paragraphSpacing: 0 + m_charWidthMaxAdj: 0 + m_enableWordWrapping: 1 + m_wordWrappingRatios: 0.4 + m_overflowMode: 0 + m_linkedTextComponent: {fileID: 0} + parentLinkedComponent: {fileID: 0} + m_enableKerning: 1 + m_enableExtraPadding: 0 + checkPaddingRequired: 0 + m_isRichText: 1 + m_parseCtrlCharacters: 1 + m_isOrthographic: 1 + m_isCullingEnabled: 0 + m_horizontalMapping: 0 + m_verticalMapping: 0 + m_uvLineOffset: 0 + m_geometrySortingOrder: 0 + m_IsTextObjectScaleStatic: 0 + m_VertexBufferAutoSizeReduction: 0 + m_useMaxVisibleDescender: 1 + m_pageToDisplay: 1 + m_margin: {x: 0, y: 0, z: 0, w: 0} + m_isUsingLegacyAnimationComponent: 0 + m_isVolumetricText: 0 + m_hasFontAssetChanged: 0 + m_baseMaterial: {fileID: 0} + m_maskOffset: {x: 0, y: 0, z: 0, w: 0} +--- !u!222 &1395840128 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1395840124} + m_CullTransparentMesh: 1 --- !u!1 &1417065444 stripped GameObject: m_CorrespondingSourceObject: {fileID: 7029954221290272535, guid: bb7ccf3dcb131ed4cbdc3e66d7f136a0, type: 3} @@ -1113,7 +1293,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: timePeriod: 0.0002 - secondsOfDay: 30000 + secondsOfDay: 60000 --- !u!4 &1861247505 Transform: m_ObjectHideFlags: 0 @@ -1198,7 +1378,8 @@ PrefabInstance: propertyPath: m_Name value: Terrain objectReference: {fileID: 0} - m_RemovedComponents: [] + m_RemovedComponents: + - {fileID: 7029954221290272539, guid: bb7ccf3dcb131ed4cbdc3e66d7f136a0, type: 3} m_SourcePrefab: {fileID: 100100000, guid: bb7ccf3dcb131ed4cbdc3e66d7f136a0, type: 3} --- !u!1001 &8390537188401440993 PrefabInstance: @@ -1207,10 +1388,34 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 0} m_Modifications: + - target: {fileID: 8390537187866079437, guid: 6a86db7765f277243b912c20f451dc47, type: 3} + propertyPath: mouseSensitivityX + value: 600 + objectReference: {fileID: 0} + - target: {fileID: 8390537187866079437, guid: 6a86db7765f277243b912c20f451dc47, type: 3} + propertyPath: mouseSensitivityY + value: 600 + objectReference: {fileID: 0} - target: {fileID: 8390537188327132961, guid: 6a86db7765f277243b912c20f451dc47, type: 3} propertyPath: interactionText value: objectReference: {fileID: 114639929} + - target: {fileID: 8390537188327133020, guid: 6a86db7765f277243b912c20f451dc47, type: 3} + propertyPath: gravity + value: -9.86 + objectReference: {fileID: 0} + - target: {fileID: 8390537188327133020, guid: 6a86db7765f277243b912c20f451dc47, type: 3} + propertyPath: jumpHeight + value: 3.5 + objectReference: {fileID: 0} + - target: {fileID: 8390537188327133020, guid: 6a86db7765f277243b912c20f451dc47, type: 3} + propertyPath: sneakSpeed + value: 0.8 + objectReference: {fileID: 0} + - target: {fileID: 8390537188327133020, guid: 6a86db7765f277243b912c20f451dc47, type: 3} + propertyPath: sprintSpeed + value: 1.1 + objectReference: {fileID: 0} - target: {fileID: 8390537188327133021, guid: 6a86db7765f277243b912c20f451dc47, type: 3} propertyPath: m_Name value: FirstPerson Player @@ -1225,7 +1430,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 8390537188327133022, guid: 6a86db7765f277243b912c20f451dc47, type: 3} propertyPath: m_LocalPosition.y - value: 21.11 + value: 25.47 objectReference: {fileID: 0} - target: {fileID: 8390537188327133022, guid: 6a86db7765f277243b912c20f451dc47, type: 3} propertyPath: m_LocalPosition.z @@ -1259,5 +1464,6 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} - m_RemovedComponents: [] + m_RemovedComponents: + - {fileID: 8390537188327133023, guid: 6a86db7765f277243b912c20f451dc47, type: 3} m_SourcePrefab: {fileID: 100100000, guid: 6a86db7765f277243b912c20f451dc47, type: 3} diff --git a/Assets/Scripts/Character.meta b/Assets/Scripts/Character.meta new file mode 100644 index 0000000..cf71a01 --- /dev/null +++ b/Assets/Scripts/Character.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d75be92c4c2783248ad8eb12442e96c4 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/MouseLook.cs b/Assets/Scripts/Character/MouseLook.cs similarity index 100% rename from Assets/Scripts/MouseLook.cs rename to Assets/Scripts/Character/MouseLook.cs diff --git a/Assets/Scripts/MouseLook.cs.meta b/Assets/Scripts/Character/MouseLook.cs.meta similarity index 100% rename from Assets/Scripts/MouseLook.cs.meta rename to Assets/Scripts/Character/MouseLook.cs.meta diff --git a/Assets/Scripts/PlayerInteraction.cs b/Assets/Scripts/Character/PlayerInteraction.cs similarity index 100% rename from Assets/Scripts/PlayerInteraction.cs rename to Assets/Scripts/Character/PlayerInteraction.cs diff --git a/Assets/Scripts/PlayerInteraction.cs.meta b/Assets/Scripts/Character/PlayerInteraction.cs.meta similarity index 100% rename from Assets/Scripts/PlayerInteraction.cs.meta rename to Assets/Scripts/Character/PlayerInteraction.cs.meta diff --git a/Assets/Scripts/Character/PlayerMovement.cs b/Assets/Scripts/Character/PlayerMovement.cs new file mode 100644 index 0000000..f4a0283 --- /dev/null +++ b/Assets/Scripts/Character/PlayerMovement.cs @@ -0,0 +1,79 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class PlayerMovement : MonoBehaviour +{ + [Header("Speed Modifier")] + [SerializeField] private float speed = 12f; + [SerializeField] private float adjustedSpeed; + [SerializeField] private float sneakSpeed = 0.4f; + [SerializeField] private float sprintSpeed = 1.8f; + [SerializeField] private float jumpHeight = 3f; + + [Header("Ground Check")] + [SerializeField] private Transform groundCheck; + [SerializeField] private float groundDistance = 0.4f; + [SerializeField] private LayerMask groundMask; + + + private Rigidbody rb; + private bool isGrounded; + private bool isSprinting = false; + private bool isSneaking = false; + Vector3 movement; + + private void Start() + { + rb = GetComponent(); + } + + private void Update() + { + //Check every frame if the player stands on the ground + isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); + + //JUMP + if(Input.GetButtonDown("Jump") && isGrounded) + { + //Add Force Up To Jump + rb.AddForce(Vector3.up * jumpHeight * 100); //Times 100 -> so we can use smaller numbers in the editor + } + + + //Sprint + if (Input.GetButton("Sprint") && isGrounded) + { + Debug.Log("[PlayerController] Sprinting"); + isSprinting = true; + adjustedSpeed *= sprintSpeed; + } + else + { + isSprinting = false; + //Sneak + if (Input.GetButton("Sneak") && isGrounded) + { + Debug.Log("[PlayerController] Sneaking"); + isSprinting = true; + adjustedSpeed *= sneakSpeed; + } + else + { + isSneaking = false; + } + } + } + + private void FixedUpdate() + { + float x = Input.GetAxis("Horizontal"); + float z = Input.GetAxis("Vertical"); + + + movement = x * adjustedSpeed * transform.right + new Vector3(0, rb.velocity.y, 0) + z * adjustedSpeed * transform.forward; + rb.velocity = movement; + adjustedSpeed = speed; + } + +} diff --git a/Assets/Scripts/PlayerMovement.cs.meta b/Assets/Scripts/Character/PlayerMovement.cs.meta similarity index 100% rename from Assets/Scripts/PlayerMovement.cs.meta rename to Assets/Scripts/Character/PlayerMovement.cs.meta diff --git a/Assets/Scripts/FPSCounter.cs b/Assets/Scripts/FPSCounter.cs new file mode 100644 index 0000000..92e336d --- /dev/null +++ b/Assets/Scripts/FPSCounter.cs @@ -0,0 +1,21 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + + +public class FPSCounter : MonoBehaviour +{ + public TMPro.TextMeshProUGUI fpsText; + public float deltaTime; + void Start() + { + fpsText = gameObject.GetComponent(); + } + + void Update() + { + deltaTime += (Time.deltaTime - deltaTime) * 0.1f; + float fps = 1.0f / deltaTime; + fpsText.text = Mathf.Ceil(fps).ToString() + "FPS"; + } +} diff --git a/Assets/Scripts/FPSCounter.cs.meta b/Assets/Scripts/FPSCounter.cs.meta new file mode 100644 index 0000000..e697081 --- /dev/null +++ b/Assets/Scripts/FPSCounter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1866e0fb446be0342a91f95d71fc2c2d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs deleted file mode 100644 index 734d303..0000000 --- a/Assets/Scripts/PlayerMovement.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class PlayerMovement : MonoBehaviour -{ - - [SerializeField] private CharacterController controller; - //[SerializeField] private Transform playerTransform; - [SerializeField] private float speed = 12f; - [SerializeField] private float airSpeed = 0.6f; - [SerializeField] private float sneakSpeed = 0.4f; - [SerializeField] private float sprintSpeed = 1.8f; - [SerializeField] private float sprintAirSpeed = 1.4f; - [SerializeField] private float gravity = -9.81f; - [SerializeField] private float jumpHeight = 3f; - - - [SerializeField] private Transform groundCheck; - [SerializeField] private float groundDistance = 0.4f; - [SerializeField] private LayerMask groundMask; - - private Vector3 velocity; - private bool isGrounded; - private bool isSprinting = false; - private bool isSneaking = false; - - void Update() - { - //GROUND CHECK - //Creates an invisible sphere on the bottom of our player - //And checks if it's colliding with something !with the "ground"-Mask in Unity! - isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); - - //Reset velocity if grounded - if (isGrounded && velocity.y < 0) - { - velocity.y = -2f; - } - - - //MOVEMENT - //Input.GetAxis is based on the Unity Input settings (edit -> Project Settings -> Input Manager) - float x = Input.GetAxis("Horizontal"); - float z = Input.GetAxis("Vertical"); - //Create move vector !in look direction! - Vector3 move; - - if (isGrounded)//for air control - { - move = transform.right * x + transform.forward * z; - - //SPRINT - if (Input.GetButton("Sprint")) - { - move *= sprintSpeed; - isSprinting = true; - } - else - { - //SNEAK - if (Input.GetButton("Sneak")) - { - move *= sneakSpeed; - isSneaking = true; - //Kommt mit character model und animations - } - else - { - isSneaking = false; - } - isSprinting = false; - } - - //JUMP - if (Input.GetButtonDown("Jump")) - { - velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); - } - } - else//Air control - { - if (isSprinting) - { - move = transform.right * x * airSpeed * sprintAirSpeed + transform.forward * z * airSpeed * sprintAirSpeed; - } - else - { - move = transform.right * x * airSpeed + transform.forward * z * airSpeed; - } - } - - //Apply move vector - controller.Move(move * speed * Time.deltaTime); - - //Add gravity to current velocity - velocity.y += gravity * Time.deltaTime; - //apply gravity - controller.Move(velocity * Time.deltaTime);//nochmal time.deltatime wegen irgendwas mit physikalischer Formel und so - - } -} diff --git a/ProjectSettings/DynamicsManager.asset b/ProjectSettings/DynamicsManager.asset index cdc1f3e..51937e5 100644 --- a/ProjectSettings/DynamicsManager.asset +++ b/ProjectSettings/DynamicsManager.asset @@ -3,10 +3,11 @@ --- !u!55 &1 PhysicsManager: m_ObjectHideFlags: 0 - serializedVersion: 11 - m_Gravity: {x: 0, y: -9.81, z: 0} + serializedVersion: 13 + m_Gravity: {x: 0, y: -12, z: 0} m_DefaultMaterial: {fileID: 0} m_BounceThreshold: 2 + m_DefaultMaxDepenetrationVelocity: 10 m_SleepThreshold: 0.005 m_DefaultContactOffset: 0.01 m_DefaultSolverIterations: 6 @@ -22,6 +23,7 @@ PhysicsManager: m_AutoSyncTransforms: 0 m_ReuseCollisionCallbacks: 1 m_ClothInterCollisionSettingsToggle: 0 + m_ClothGravity: {x: 0, y: -9.81, z: 0} m_ContactPairsMode: 0 m_BroadphaseType: 0 m_WorldBounds: @@ -31,4 +33,5 @@ PhysicsManager: m_FrictionType: 0 m_EnableEnhancedDeterminism: 0 m_EnableUnifiedHeightmaps: 1 - m_DefaultMaxAngluarSpeed: 7 + m_SolverType: 0 + m_DefaultMaxAngularSpeed: 7