From 18d90a21195aef1ba332e5bea4da46fecf0057b7 Mon Sep 17 00:00:00 2001 From: juliuse98 Date: Thu, 21 Oct 2021 22:03:07 +0200 Subject: [PATCH 1/4] Fixed Neck Fixed the Player Controller Prefab --- Assets/Player.prefab | 55 ++++++++++++++++++---- Assets/Scenes/TestScene.unity | 57 +++++++++++++++++++++++ Assets/Scripts/Player/PlayerController.cs | 42 +++++++---------- UserSettings/Search.index | 13 ++++++ UserSettings/Search.settings | 54 ++++++++++++++++++++- 5 files changed, 186 insertions(+), 35 deletions(-) create mode 100644 UserSettings/Search.index diff --git a/Assets/Player.prefab b/Assets/Player.prefab index 3ce7d7e..ccbe389 100644 --- a/Assets/Player.prefab +++ b/Assets/Player.prefab @@ -32,8 +32,8 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - {fileID: 2458562619286483438} - - {fileID: 6272346181657429396} - {fileID: 6272346182417644039} + - {fileID: 223626088105882255} m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -49,7 +49,7 @@ CharacterController: m_Enabled: 1 serializedVersion: 2 m_Height: 2 - m_Radius: 0.5 + m_Radius: 0.3 m_SlopeLimit: 45 m_StepOffset: 0.1 m_SkinWidth: 0.08 @@ -68,10 +68,16 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: playerCamera: {fileID: 6272346181657429396} + playerNeck: {fileID: 223626088105882255} mouseSensitivity: 4 + neckStartAngle: 0 + maxCameraAngle: 90 + minCameraAngle: -90 + neckLength: 0.2 mouseSmoothTime: 0 lockCursor: 1 walkSpeed: 6 + sprintSpeed: 10 moveSmoothTime: 0.05 gravity: -13 jumpHeight: 1 @@ -138,13 +144,13 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 6272346181657429393} - m_LocalRotation: {x: -0, y: -0.67557263, z: -0, w: 0.7372934} - m_LocalPosition: {x: 0, y: 2, z: 0} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0.3, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] - m_Father: {fileID: 6272346181302961296} - m_RootOrder: 1 - m_LocalEulerAnglesHint: {x: 0, y: -84.997, z: 0} + m_Father: {fileID: 223626088105882255} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!20 &6272346181657429395 Camera: m_ObjectHideFlags: 0 @@ -224,6 +230,37 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 6272346181302961296} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &7697277603114630017 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 223626088105882255} + m_Layer: 0 + m_Name: Neck + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &223626088105882255 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7697277603114630017} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 1.8, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: + - {fileID: 6272346181657429396} + m_Father: {fileID: 6272346181302961296} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &8690889598169456425 @@ -274,7 +311,7 @@ MeshRenderer: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 8690889598169456425} - m_Enabled: 1 + m_Enabled: 0 m_CastShadows: 1 m_ReceiveShadows: 1 m_DynamicOccludee: 1 @@ -318,7 +355,7 @@ CapsuleCollider: m_GameObject: {fileID: 8690889598169456425} m_Material: {fileID: 0} m_IsTrigger: 0 - m_Enabled: 1 + m_Enabled: 0 m_Radius: 0.5 m_Height: 2 m_Direction: 1 diff --git a/Assets/Scenes/TestScene.unity b/Assets/Scenes/TestScene.unity index 0d01e60..d59cd39 100644 --- a/Assets/Scenes/TestScene.unity +++ b/Assets/Scenes/TestScene.unity @@ -1069,6 +1069,63 @@ MeshCollider: m_Convex: 0 m_CookingOptions: 30 m_Mesh: {fileID: 8552134717624371188, guid: 823e8b39d52b71b4eb5a91dbc8d6d59e, type: 3} +--- !u!1001 &1880657102 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 6272346181302961293, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: m_Name + value: Player + objectReference: {fileID: 0} + - target: {fileID: 6272346181302961296, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: m_RootOrder + value: 6 + objectReference: {fileID: 0} + - target: {fileID: 6272346181302961296, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: m_LocalPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6272346181302961296, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: m_LocalPosition.y + value: 2.15 + objectReference: {fileID: 0} + - target: {fileID: 6272346181302961296, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: m_LocalPosition.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6272346181302961296, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 6272346181302961296, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6272346181302961296, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6272346181302961296, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6272346181302961296, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6272346181302961296, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6272346181302961296, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} --- !u!1 &1960893053 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index 9dd5596..0ddf024 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -12,9 +12,9 @@ public class PlayerController : MonoBehaviour [SerializeField] private Transform playerNeck = null; [SerializeField] private float mouseSensitivity = 4.0f; - [SerializeField] private float maxCameraAngle = -90f; [SerializeField] private float neckStartAngle = 0f; - [SerializeField] private float minCameraAngle = 90f; + [SerializeField] private float maxCameraAngle = 90f; + [SerializeField] private float minCameraAngle = -90f; [SerializeField] private float neckLength = 0.2f; [SerializeField] [Range(0.0f, 0.5f)] private float mouseSmoothTime = 0.001f; @@ -22,6 +22,7 @@ public class PlayerController : MonoBehaviour [Header("Movement")] [SerializeField] private float walkSpeed = 6.0f; + [SerializeField] private float sprintSpeed = 10.0f; [SerializeField][Range(0.0f, 0.5f)] private float moveSmoothTime = 0.05f; [SerializeField] float gravity = -13.0f; [SerializeField] private float jumpHeight; @@ -32,9 +33,7 @@ public class PlayerController : MonoBehaviour [SerializeField] private LayerMask groundMask; public bool isGrounded; - private float fullPitch = 0f; - private float cameraPitch = 0f; - private float neckPitch = 0f; + private float viewPitch = 0f; private float velocityY = 0.0f; private CharacterController controller; @@ -53,6 +52,10 @@ public class PlayerController : MonoBehaviour Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } + + //Position the camera at the height of the neck (set by the neckLength) + playerCamera.position = playerNeck.position; + playerCamera.position += playerNeck.up * neckLength; } private void Update() { @@ -70,30 +73,19 @@ public class PlayerController : MonoBehaviour Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); //Get the axis of the mouse currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime); - fullPitch -= currentMouseDelta.y * mouseSensitivity; - fullPitch = Mathf.Clamp(fullPitch,-maxCameraAngle,-minCameraAngle); - //cameraPitch = Mathf.Clamp(fullPitch, -90, 45); - //neckPitch = Mathf.Clamp(fullPitch, 45, 90); - Debug.Log("fullPitch: " + fullPitch); + viewPitch -= currentMouseDelta.y * mouseSensitivity; - if (fullPitch >= neckStartAngle) { - playerNeck.localEulerAngles = Vector3.right * (fullPitch - neckStartAngle); - + viewPitch = Mathf.Clamp(viewPitch,-maxCameraAngle,-minCameraAngle); + + if (viewPitch >= neckStartAngle) + { + playerNeck.localEulerAngles = Vector3.right * (viewPitch - neckStartAngle); } - else { - playerCamera.localEulerAngles = Vector3.right * fullPitch; - - + else + { + playerCamera.localEulerAngles = Vector3.right * viewPitch; } - playerCamera.position = playerNeck.position; - playerCamera.position += playerNeck.up * neckLength; - /* - playerNeck.localEulerAngles = Vector3.right * neckPitch; - playerCamera.localEulerAngles = Vector3.right * cameraPitch; - playerCamera.position = playerNeck.position; - playerCamera.position += playerNeck.up * neckLength;*/ - //playerCamera.RotateAround(playerNeck.position, Vector3.right, cameraPitch* Mathf.Deg2Rad - playerCamera.transform.rotation.x); transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity); //Rotate the hole player if looked sideways (Rotates the player left and right) } diff --git a/UserSettings/Search.index b/UserSettings/Search.index new file mode 100644 index 0000000..299c246 --- /dev/null +++ b/UserSettings/Search.index @@ -0,0 +1,13 @@ +{ + "name": "Assets", + "roots": ["Assets"], + "includes": [], + "excludes": ["Temp/", "External/"], + "options": { + "types": true, + "properties": false, + "extended": false, + "dependencies": false + }, + "baseScore": 999 +} \ No newline at end of file diff --git a/UserSettings/Search.settings b/UserSettings/Search.settings index 9e26dfe..eda2089 100644 --- a/UserSettings/Search.settings +++ b/UserSettings/Search.settings @@ -1 +1,53 @@ -{} \ No newline at end of file +trackSelection = true +fetchPreview = true +wantsMore = false +keepOpen = false +queryFolder = "Assets" +onBoardingDoNotAskAgain = true +showPackageIndexes = false +showStatusBar = false +scopes = { +} +providers = { + asset = { + active = true + priority = 25 + defaultAction = null + } + store = { + active = true + priority = 100 + defaultAction = null + } + find = { + active = true + priority = 25 + defaultAction = null + } + log = { + active = false + priority = 210 + defaultAction = null + } + packages = { + active = true + priority = 90 + defaultAction = null + } + scene = { + active = true + priority = 50 + defaultAction = null + } + query = { + active = true + priority = 100 + defaultAction = null + } +} +recentSearches = [ +] +searchItemFavorites = [ +] +savedSearchesSortOrder = 0 +showSavedSearchPanel = false \ No newline at end of file From e59f509df610c764782a7baa308cd9fcb2cb741f Mon Sep 17 00:00:00 2001 From: juliuse98 Date: Fri, 22 Oct 2021 11:09:11 +0200 Subject: [PATCH 2/4] PlayerController -Set up the player Prefab -Added sprinting -Added speed up and speed down --- Assets/Player.prefab | 23 ++++------------ Assets/Scenes/TestScene.unity | 16 +++++++++++ Assets/Scripts/Player/PlayerController.cs | 33 +++++++++++++++++++++-- ProjectSettings/InputManager.asset | 16 +++++++++++ 4 files changed, 68 insertions(+), 20 deletions(-) diff --git a/Assets/Player.prefab b/Assets/Player.prefab index ccbe389..10f7748 100644 --- a/Assets/Player.prefab +++ b/Assets/Player.prefab @@ -78,11 +78,13 @@ MonoBehaviour: lockCursor: 1 walkSpeed: 6 sprintSpeed: 10 + speedUpVal: 0.3 + speedDownVal: 0.1 moveSmoothTime: 0.05 gravity: -13 jumpHeight: 1 groundCheck: {fileID: 6272346182417644039} - groundDistance: 0.2 + groundDistance: 0.3 groundMask: serializedVersion: 2 m_Bits: 64 @@ -174,7 +176,7 @@ Camera: y: 0 width: 1 height: 1 - near clip plane: 0.3 + near clip plane: 0.15 far clip plane: 1000 field of view: 61.4 orthographic: 0 @@ -274,7 +276,6 @@ GameObject: - component: {fileID: 2458562619286483438} - component: {fileID: 390397068222218691} - component: {fileID: 8824871639981994016} - - component: {fileID: 5472825146870239901} m_Layer: 0 m_Name: Capsule m_TagString: Untagged @@ -311,7 +312,7 @@ MeshRenderer: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 8690889598169456425} - m_Enabled: 0 + m_Enabled: 1 m_CastShadows: 1 m_ReceiveShadows: 1 m_DynamicOccludee: 1 @@ -346,17 +347,3 @@ MeshRenderer: m_SortingLayer: 0 m_SortingOrder: 0 m_AdditionalVertexStreams: {fileID: 0} ---- !u!136 &5472825146870239901 -CapsuleCollider: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 8690889598169456425} - m_Material: {fileID: 0} - m_IsTrigger: 0 - m_Enabled: 0 - m_Radius: 0.5 - m_Height: 2 - m_Direction: 1 - m_Center: {x: 0, y: 0, z: 0} diff --git a/Assets/Scenes/TestScene.unity b/Assets/Scenes/TestScene.unity index d59cd39..a480d39 100644 --- a/Assets/Scenes/TestScene.unity +++ b/Assets/Scenes/TestScene.unity @@ -1080,6 +1080,18 @@ PrefabInstance: propertyPath: m_Name value: Player objectReference: {fileID: 0} + - target: {fileID: 6272346181302961294, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: speedUpVal + value: 0.3 + objectReference: {fileID: 0} + - target: {fileID: 6272346181302961294, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: sprintSpeed + value: 10 + objectReference: {fileID: 0} + - target: {fileID: 6272346181302961294, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: groundDistance + value: 0.3 + objectReference: {fileID: 0} - target: {fileID: 6272346181302961296, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} propertyPath: m_RootOrder value: 6 @@ -1124,6 +1136,10 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} + - target: {fileID: 6272346181657429395, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} + propertyPath: near clip plane + value: 0.15 + objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 2935eead9a075fd489d6a6dc273a5999, type: 3} --- !u!1 &1960893053 diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index 0ddf024..8d8fa6c 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -23,6 +23,8 @@ public class PlayerController : MonoBehaviour [Header("Movement")] [SerializeField] private float walkSpeed = 6.0f; [SerializeField] private float sprintSpeed = 10.0f; + [SerializeField]private float speedUpVal = 0.2f; + [SerializeField]private float speedDownVal = 0.1f; [SerializeField][Range(0.0f, 0.5f)] private float moveSmoothTime = 0.05f; [SerializeField] float gravity = -13.0f; [SerializeField] private float jumpHeight; @@ -35,6 +37,7 @@ public class PlayerController : MonoBehaviour public bool isGrounded; private float viewPitch = 0f; private float velocityY = 0.0f; + private float speed; private CharacterController controller; private Vector2 currentDir = Vector2.zero; @@ -66,7 +69,9 @@ public class PlayerController : MonoBehaviour private void Grounded() { //Check every frame if the player stands on the ground - isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); + Vector3 groundCheckPos = groundCheck.position; + groundCheckPos += new Vector3(0,0,0); + isGrounded = Physics.CheckSphere(groundCheckPos, groundDistance, groundMask); } private void UpdateMouseLook() { @@ -108,8 +113,32 @@ public class PlayerController : MonoBehaviour currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime); //Smooth movement change + - Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + (Vector3.up * velocityY); + if (Input.GetButton("Sprint") && isGrounded) + { //If Sprint button is pressed the speed is switched form walking to sprinting + if (speed <= sprintSpeed) + { + speed += sprintSpeed * speedUpVal; + } + else + { + speed = sprintSpeed; + } + } + else + { + if (speed <= sprintSpeed) + { + speed += walkSpeed * speedDownVal; + } + else + { + speed = walkSpeed; + } + } + + Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * speed + (Vector3.up * velocityY); controller.Move(velocity * Time.deltaTime); diff --git a/ProjectSettings/InputManager.asset b/ProjectSettings/InputManager.asset index 17c8f53..35b00f0 100644 --- a/ProjectSettings/InputManager.asset +++ b/ProjectSettings/InputManager.asset @@ -293,3 +293,19 @@ InputManager: type: 0 axis: 0 joyNum: 0 + - serializedVersion: 3 + m_Name: Sprint + descriptiveName: + descriptiveNegativeName: + negativeButton: + positiveButton: left shift + altNegativeButton: + altPositiveButton: joystick button 1 + gravity: 1000 + dead: 0.001 + sensitivity: 1000 + snap: 0 + invert: 0 + type: 0 + axis: 0 + joyNum: 0 From b8a4d57c27b37eda95f1a1dff32cec3c1234622f Mon Sep 17 00:00:00 2001 From: juliuse98 Date: Sun, 31 Oct 2021 22:28:22 +0100 Subject: [PATCH 3/4] Player Controller -Added dedicated "Mouse Look" script -Added Ground Angle function --- Assets/Player.prefab | 82 +++++++---------- Assets/Scripts/Player/PlayerController.cs | 89 +++++++------------ Assets/Scripts/Player/PlayerMouseLook.cs | 82 +++++++++++++++++ Assets/Scripts/Player/PlayerMouseLook.cs.meta | 11 +++ ProjectSettings/ProjectVersion.txt | 4 +- UserSettings/EditorUserSettings.asset | 4 +- 6 files changed, 159 insertions(+), 113 deletions(-) create mode 100644 Assets/Scripts/Player/PlayerMouseLook.cs create mode 100644 Assets/Scripts/Player/PlayerMouseLook.cs.meta diff --git a/Assets/Player.prefab b/Assets/Player.prefab index 0dd6005..87e45e0 100644 --- a/Assets/Player.prefab +++ b/Assets/Player.prefab @@ -26,7 +26,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 681358948229890261} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0.3586286, y: -0.5799999, z: -0.03139314} + m_LocalPosition: {x: 0, y: -0.5799999, z: 0.27} m_LocalScale: {x: 0.8, y: 0.2, z: 1} m_Children: [] m_Father: {fileID: 981273033772084037} @@ -266,8 +266,8 @@ GameObject: - component: {fileID: 320375778840406984} - component: {fileID: -4574526707603160507} - component: {fileID: 7273209952621479910} - - component: {fileID: 5317111308216027170} - component: {fileID: 4959463831902427506} + - component: {fileID: 5630859218361338035} m_Layer: 0 m_Name: Player m_TagString: Player @@ -304,10 +304,10 @@ CharacterController: m_IsTrigger: 0 m_Enabled: 1 serializedVersion: 2 - m_Height: 2 - m_Radius: 0.5 + m_Height: 1.8 + m_Radius: 0.3 m_SlopeLimit: 45 - m_StepOffset: 0.1 + m_StepOffset: 0.2 m_SkinWidth: 0.08 m_MinMoveDistance: 0.001 m_Center: {x: 0, y: 1, z: 0} @@ -342,21 +342,11 @@ MonoBehaviour: m_EditorClassIdentifier: syncMode: 0 syncInterval: 0.1 - playerCamera: {fileID: 6272346181657429396} - playerNeck: {fileID: 981273033772084037} - mouseSensitivity: 4 - maxCameraAngle: 90 - neckStartAngle: 0 - minCameraAngle: -90 - neckLength: 0.2 - mouseSmoothTime: 0.001 - lockCursor: 1 walkSpeed: 6 moveSmoothTime: 0.05 gravity: -13 jumpHeight: 2 groundCheck: {fileID: 6272346182417644039} - groundDistance: 0.4 groundMask: serializedVersion: 2 m_Bits: 64 @@ -390,24 +380,6 @@ MonoBehaviour: showGizmos: 1 showOverlay: 1 overlayColor: {r: 0, g: 0, b: 0, a: 0.5} ---- !u!114 &5317111308216027170 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 6272346181302961293} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: c2e457ad00c1a044a924a5bc00776df9, type: 3} - m_Name: - m_EditorClassIdentifier: - syncMode: 0 - syncInterval: 0.1 - isAlive: 0 - clientId: 0 - username: Janis - usernameTextObj: {fileID: 4824770690127859843} --- !u!114 &4959463831902427506 MonoBehaviour: m_ObjectHideFlags: 0 @@ -424,6 +396,29 @@ MonoBehaviour: syncInterval: 0.1 color: {r: 1, g: 0, b: 0, a: 0} renderer: {fileID: 8824871639981994016} +--- !u!114 &5630859218361338035 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6272346181302961293} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a8493f0e2b0d4bd4f98d3cd81c2db5ad, type: 3} + m_Name: + m_EditorClassIdentifier: + syncMode: 0 + syncInterval: 0.1 + playerCamera: {fileID: 6272346181657429396} + playerNeck: {fileID: 981273033772084037} + mouseSensitivity: 4 + maxCameraAngle: 90 + neckStartAngle: 0 + minCameraAngle: -90 + neckLength: 0.2 + mouseSmoothTime: 0.001 + lockCursor: 1 --- !u!1 &6272346181657429393 GameObject: m_ObjectHideFlags: 0 @@ -478,7 +473,7 @@ Camera: y: 0 width: 1 height: 1 - near clip plane: 0.3 + near clip plane: 0.1 far clip plane: 1000 field of view: 61.4 orthographic: 0 @@ -522,7 +517,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 6272346182417644038} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: -0, z: 0} + m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 6272346181302961296} @@ -571,7 +566,6 @@ GameObject: - component: {fileID: 2458562619286483438} - component: {fileID: 390397068222218691} - component: {fileID: 8824871639981994016} - - component: {fileID: 5472825146870239901} m_Layer: 0 m_Name: Capsule m_TagString: Untagged @@ -608,7 +602,7 @@ MeshRenderer: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 8690889598169456425} - m_Enabled: 1 + m_Enabled: 0 m_CastShadows: 1 m_ReceiveShadows: 1 m_DynamicOccludee: 1 @@ -643,17 +637,3 @@ MeshRenderer: m_SortingLayer: 0 m_SortingOrder: 0 m_AdditionalVertexStreams: {fileID: 0} ---- !u!136 &5472825146870239901 -CapsuleCollider: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 8690889598169456425} - m_Material: {fileID: 0} - m_IsTrigger: 0 - m_Enabled: 1 - m_Radius: 0.5 - m_Height: 2 - m_Direction: 1 - m_Center: {x: 0, y: 0, z: 0} diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index de59eef..4289dad 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -8,97 +8,70 @@ using Mirror; public class PlayerController : NetworkBehaviour { - [Header("Mouse Look")] - [SerializeField] private Transform playerCamera = null; - [SerializeField] private Transform playerNeck = null; - [SerializeField] private float mouseSensitivity = 4.0f; - - [SerializeField] private float maxCameraAngle = -90f; - [SerializeField] private float neckStartAngle = 0f; - [SerializeField] private float minCameraAngle = 90f; - - [SerializeField] private float neckLength = 0.2f; - [SerializeField] [Range(0.0f, 0.5f)] private float mouseSmoothTime = 0.001f; - [SerializeField] private bool lockCursor = true; [Header("Movement")] [SerializeField] private float walkSpeed = 6.0f; [SerializeField][Range(0.0f, 0.5f)] private float moveSmoothTime = 0.05f; [SerializeField] float gravity = -13.0f; [SerializeField] private float jumpHeight; + private Vector3 inputDirection = Vector3.zero; + private Vector3 moveDirection; [Header("Ground Check")] [SerializeField] private Transform groundCheck; - [SerializeField] private float groundDistance = 0.4f; [SerializeField] private LayerMask groundMask; + [Header("Ground Angle")] + // [SerializeField] private Transform groundDistance; + + public bool isGrounded; - private float fullPitch = 0f; - private float cameraPitch = 0f; - private float neckPitch = 0f; private float velocityY = 0.0f; private CharacterController controller; private Vector2 currentDir = Vector2.zero; private Vector2 currentDirVelocity = Vector2.zero; - private Vector2 currentMouseDelta = Vector2.zero; - private Vector2 currentMouseDeltaVelocity = Vector2.zero; - private void Start() { if (isLocalPlayer) { controller = GetComponent(); - if (lockCursor) - { - Cursor.lockState = CursorLockMode.Locked; - Cursor.visible = false; - } } - else - { - playerCamera.gameObject.SetActive(false); - } - } private void Update() { - if (!isLocalPlayer) return; - - UpdateMouseLook(); - Grounded(); - UpdateMovement(); - + if (isLocalPlayer) + { + Grounded(); + CheckGoundAngle(); + UpdateMovement(); + } } private void Grounded() { //Check every frame if the player stands on the ground - isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); + isGrounded = Physics.CheckSphere(groundCheck.position + new Vector3(0, GetComponent().radius - 0.01f, 0),GetComponent().radius + 0.0f, groundMask); } - private void UpdateMouseLook() + + private void CheckGoundAngle() { - Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); //Get the axis of the mouse - - currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime); - fullPitch -= currentMouseDelta.y * mouseSensitivity; - fullPitch = Mathf.Clamp(fullPitch,-maxCameraAngle,-minCameraAngle); - - if (fullPitch >= neckStartAngle) { - playerNeck.localEulerAngles = Vector3.right * (fullPitch - neckStartAngle); - + //Check every frame if the player stands on the ground + RaycastHit hit; + if (Physics.Raycast(groundCheck.position + new Vector3(0,0.4f,0),Vector3.down,out hit)) + { + Debug.Log(transform.eulerAngles.y); + moveDirection = Quaternion.Euler(0, transform.eulerAngles.y + 90, 0) * inputDirection; + moveDirection = Vector3.Cross(moveDirection, hit.normal); } - else { - playerCamera.localEulerAngles = Vector3.right * fullPitch; - - - } - playerCamera.position = playerNeck.position; - playerCamera.position += playerNeck.up * neckLength; - - transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity); //Rotate the hole player if looked sideways (Rotates the player left and right) } + private void OnDrawGizmos() + { + Gizmos.color = Color.red; + Gizmos.DrawRay(new Ray(transform.position, moveDirection * 50)); + } + private void UpdateMovement() { //Grounded @@ -113,10 +86,10 @@ public class PlayerController : NetworkBehaviour velocityY += Mathf.Sqrt(jumpHeight * -2f * gravity); } - Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); //Get Inputs - targetDir.Normalize(); //Damit schräg laufen nicht schneller ist + inputDirection = new Vector3(Input.GetAxisRaw("Horizontal"),0, Input.GetAxisRaw("Vertical")); //Get Inputs + inputDirection.Normalize(); //Damit schräg laufen nicht schneller ist - currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime); //Smooth movement change + currentDir = Vector2.SmoothDamp(currentDir, new Vector2(inputDirection.x,inputDirection.z), ref currentDirVelocity, moveSmoothTime); //Smooth movement change Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + (Vector3.up * velocityY); diff --git a/Assets/Scripts/Player/PlayerMouseLook.cs b/Assets/Scripts/Player/PlayerMouseLook.cs new file mode 100644 index 0000000..388beed --- /dev/null +++ b/Assets/Scripts/Player/PlayerMouseLook.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using Mirror; + +public class PlayerMouseLook : NetworkBehaviour +{ + [Header("Mouse Look")] + [SerializeField] private Transform playerCamera = null; + [SerializeField] private Transform playerNeck = null; + [SerializeField] private float mouseSensitivity = 4.0f; + + [SerializeField] private float maxCameraAngle = 90f; + [SerializeField] private float neckStartAngle = 0f; + [SerializeField] private float minCameraAngle = -90f; + + [SerializeField] private float neckLength = 0.2f; + [SerializeField] [Range(0.0f, 0.5f)] private float mouseSmoothTime = 0.001f; + [SerializeField] private bool lockCursor = true; + + private float fullPitch = 0f; + private float cameraPitch = 0f; + private float neckPitch = 0f; + private float velocityY = 0.0f; + private CharacterController controller; + + private Vector2 currentMouseDelta = Vector2.zero; + private Vector2 currentMouseDeltaVelocity = Vector2.zero; + + + + private void Start() + { + if (isLocalPlayer) + { + controller = GetComponent(); + + playerCamera.gameObject.SetActive(true); + + if (lockCursor) + { + Cursor.lockState = CursorLockMode.Locked; + Cursor.visible = false; + } + } + + } + private void Update() + { + if (isLocalPlayer) + { + UpdateMouseLook(); + } + + } + + private void UpdateMouseLook() + { + Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); //Get the axis of the mouse + + currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime); + fullPitch -= currentMouseDelta.y * mouseSensitivity; + fullPitch = Mathf.Clamp(fullPitch, -maxCameraAngle, -minCameraAngle); + + if (fullPitch >= neckStartAngle) + { + playerNeck.localEulerAngles = Vector3.right * (fullPitch - neckStartAngle); + + } + else + { + playerCamera.localEulerAngles = Vector3.right * fullPitch; + + + } + playerCamera.position = playerNeck.position; + playerCamera.position += playerNeck.up * neckLength; + + transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity); //Rotate the hole player if looked sideways (Rotates the player left and right) + } + +} diff --git a/Assets/Scripts/Player/PlayerMouseLook.cs.meta b/Assets/Scripts/Player/PlayerMouseLook.cs.meta new file mode 100644 index 0000000..3d02c8d --- /dev/null +++ b/Assets/Scripts/Player/PlayerMouseLook.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a8493f0e2b0d4bd4f98d3cd81c2db5ad +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index be54ac2..5c25ebf 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2021.1.26f1 -m_EditorVersionWithRevision: 2021.1.26f1 (6f076387c01d) +m_EditorVersion: 2021.1.24f1 +m_EditorVersionWithRevision: 2021.1.24f1 (6667702a1e7c) diff --git a/UserSettings/EditorUserSettings.asset b/UserSettings/EditorUserSettings.asset index 2548d89..08a0f12 100644 --- a/UserSettings/EditorUserSettings.asset +++ b/UserSettings/EditorUserSettings.asset @@ -9,10 +9,10 @@ EditorUserSettings: value: 22424703114646680e0b0227036c6c111b07142f1f2b233e2867083debf42d flags: 0 RecentlyUsedScenePath-1: - value: 22424703114646680e0b0227036c6b1505032b292926237e38271427fb + value: 22424703114646680e0b0227036c6b150503372c2a242f3e281a1e36ece57a2decee22f0 flags: 0 RecentlyUsedScenePath-2: - value: 22424703114646680e0b0227036c6b150503372c2a242f3e281a1e36ece57a2decee22f0 + value: 22424703114646680e0b0227036c6b1505032b292926237e38271427fb flags: 0 vcSharedLogLevel: value: 0d5e400f0650 From 314c3faf3081a42bf8291a36dffc05a2d541a956 Mon Sep 17 00:00:00 2001 From: juliuse98 Date: Mon, 1 Nov 2021 14:30:00 +0100 Subject: [PATCH 4/4] Ground Angle Check -Added GroundAngleCheck --- Assets/Player.prefab | 6 ++- Assets/Scenes/TestScene.unity | 43 ++++++++++++++++++ Assets/Scripts/Player/PlayerController.cs | 53 +++++++++++++++++------ 3 files changed, 87 insertions(+), 15 deletions(-) diff --git a/Assets/Player.prefab b/Assets/Player.prefab index 87e45e0..b35461e 100644 --- a/Assets/Player.prefab +++ b/Assets/Player.prefab @@ -344,12 +344,14 @@ MonoBehaviour: syncInterval: 0.1 walkSpeed: 6 moveSmoothTime: 0.05 - gravity: -13 - jumpHeight: 2 + gravity: -10 + jumpHeight: 1 groundCheck: {fileID: 6272346182417644039} groundMask: serializedVersion: 2 m_Bits: 64 + groundAngle: 0 + moveGroundAngle: 0 isGrounded: 0 --- !u!114 &7273209952621479910 MonoBehaviour: diff --git a/Assets/Scenes/TestScene.unity b/Assets/Scenes/TestScene.unity index b1ad3a9..2f1bd86 100644 --- a/Assets/Scenes/TestScene.unity +++ b/Assets/Scenes/TestScene.unity @@ -621,6 +621,49 @@ PrefabInstance: objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 823e8b39d52b71b4eb5a91dbc8d6d59e, type: 3} +--- !u!1 &897280304 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 897280306} + - component: {fileID: 897280305} + m_Layer: 0 + m_Name: Player Spawn + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &897280305 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 897280304} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 41f84591ce72545258ea98cb7518d8b9, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!4 &897280306 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 897280304} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 10.14, y: 2.95154, z: 7.02} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 6 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1035340512 stripped GameObject: m_CorrespondingSourceObject: {fileID: -208595431880416365, guid: 823e8b39d52b71b4eb5a91dbc8d6d59e, type: 3} diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index 4289dad..9ac1965 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -11,8 +11,8 @@ public class PlayerController : NetworkBehaviour [Header("Movement")] [SerializeField] private float walkSpeed = 6.0f; - [SerializeField][Range(0.0f, 0.5f)] private float moveSmoothTime = 0.05f; - [SerializeField] float gravity = -13.0f; + [SerializeField][Range(0.0f, 0.5f)] private float moveSmoothTime = 0.001f; + [SerializeField] float gravity = -10.0f; [SerializeField] private float jumpHeight; private Vector3 inputDirection = Vector3.zero; private Vector3 moveDirection; @@ -22,15 +22,16 @@ public class PlayerController : NetworkBehaviour [SerializeField] private LayerMask groundMask; [Header("Ground Angle")] - // [SerializeField] private Transform groundDistance; - + [SerializeField] private float groundAngle; + [SerializeField] private float moveGroundAngle; public bool isGrounded; private float velocityY = 0.0f; private CharacterController controller; - private Vector2 currentDir = Vector2.zero; - private Vector2 currentDirVelocity = Vector2.zero; + private Vector3 currentDir = Vector3.zero; + private Vector3 currentDirVelocity = Vector3.zero; + private Vector3 velocity = Vector3.zero; private void Start() @@ -52,18 +53,35 @@ public class PlayerController : NetworkBehaviour private void Grounded() { //Check every frame if the player stands on the ground - isGrounded = Physics.CheckSphere(groundCheck.position + new Vector3(0, GetComponent().radius - 0.01f, 0),GetComponent().radius + 0.0f, groundMask); + isGrounded = Physics.CheckSphere(groundCheck.position + new Vector3(0, GetComponent().radius - 0.1f, 0),GetComponent().radius + 0.0f, groundMask); + } + + public bool isMoving() + { + if (velocity.x == 0 && velocity.y == 0 && velocity.z == 0) return true; + else return false; } private void CheckGoundAngle() { //Check every frame if the player stands on the ground RaycastHit hit; - if (Physics.Raycast(groundCheck.position + new Vector3(0,0.4f,0),Vector3.down,out hit)) + if (Physics.Raycast(groundCheck.position + new Vector3(0, 0.4f, 0), Vector3.down, out hit)) { - Debug.Log(transform.eulerAngles.y); - moveDirection = Quaternion.Euler(0, transform.eulerAngles.y + 90, 0) * inputDirection; + moveDirection = Quaternion.Euler(0, transform.eulerAngles.y + 90f, 0) * inputDirection; moveDirection = Vector3.Cross(moveDirection, hit.normal); + + if (isMoving()) + { + moveGroundAngle = -(Vector3.Angle(moveDirection, transform.up) - 90f); + } + else + { + moveGroundAngle = 0f; + } + + groundAngle = Vector3.Angle(hit.normal,transform.up); + Debug.Log(moveGroundAngle); } } private void OnDrawGizmos() @@ -83,16 +101,25 @@ public class PlayerController : NetworkBehaviour if (Input.GetButtonDown("Jump") && isGrounded) { //Debug.Log("Jump"); - velocityY += Mathf.Sqrt(jumpHeight * -2f * gravity); + velocityY += Mathf.Sqrt(jumpHeight * 4f); } inputDirection = new Vector3(Input.GetAxisRaw("Horizontal"),0, Input.GetAxisRaw("Vertical")); //Get Inputs inputDirection.Normalize(); //Damit schräg laufen nicht schneller ist - currentDir = Vector2.SmoothDamp(currentDir, new Vector2(inputDirection.x,inputDirection.z), ref currentDirVelocity, moveSmoothTime); //Smooth movement change + if (isGrounded) + { + currentDir = moveDirection; + } + else + { + moveDirection = new Vector3(moveDirection.x, 0, moveDirection.z); + currentDir = moveDirection; + } + currentDir = currentDir + new Vector3(0, velocityY, 0); + velocity = currentDir * walkSpeed; - Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + (Vector3.up * velocityY); controller.Move(velocity * Time.deltaTime);