From 35dd1d89020ca292c4d7eb406d04e9e5d85d3c6b Mon Sep 17 00:00:00 2001 From: Santi Date: Mon, 29 Apr 2024 23:19:13 +0100 Subject: [PATCH] Unity: Pushed working movement script Fixed issues like character not jumping --- Assets/Scenes/Character.unity | 60 +++++++++++++++++++----------- Assets/Scripts/Movement.cs | 69 +++++++++++++++++------------------ 2 files changed, 71 insertions(+), 58 deletions(-) diff --git a/Assets/Scenes/Character.unity b/Assets/Scenes/Character.unity index 0a4086c..13b55a8 100644 --- a/Assets/Scenes/Character.unity +++ b/Assets/Scenes/Character.unity @@ -358,8 +358,9 @@ GameObject: - component: {fileID: 765999531} - component: {fileID: 765999530} - component: {fileID: 765999535} - - component: {fileID: 765999534} - component: {fileID: 765999536} + - component: {fileID: 765999537} + - component: {fileID: 765999538} m_Layer: 0 m_Name: Capsule m_TagString: Player @@ -439,7 +440,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 765999529} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -0.21045552, y: 2.21, z: 0.096935526} + m_LocalPosition: {x: 0, y: 2, z: -0.5} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: @@ -447,24 +448,6 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!143 &765999534 -CharacterController: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 765999529} - m_Material: {fileID: 0} - m_IsTrigger: 0 - m_Enabled: 1 - serializedVersion: 2 - m_Height: 2 - m_Radius: 0.5 - m_SlopeLimit: 45 - m_StepOffset: 0.3 - m_SkinWidth: 0.08 - m_MinMoveDistance: 0.001 - m_Center: {x: 0, y: 0, z: 0} --- !u!114 &765999535 MonoBehaviour: m_ObjectHideFlags: 0 @@ -483,7 +466,7 @@ MonoBehaviour: jumpForce: 2.5 gravity: 9.8 throwForce: {fileID: 765999536} - controller: {fileID: 0} + controller: {fileID: 765999538} moveDirection: {x: 0, y: 0, z: 0} forwardsKey: 119 backwardsKey: 115 @@ -510,7 +493,40 @@ MonoBehaviour: previousObject: {fileID: 0} tempObject: {fileID: 0} throwForce: 5 - interactKey: 102 +--- !u!54 &765999537 +Rigidbody: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 765999529} + 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!143 &765999538 +CharacterController: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 765999529} + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_Enabled: 1 + serializedVersion: 2 + m_Height: 2 + m_Radius: 0.5 + m_SlopeLimit: 45 + m_StepOffset: 0.3 + m_SkinWidth: 0.08 + m_MinMoveDistance: 0.001 + m_Center: {x: 0, y: 0, z: 0} --- !u!1 &876529135 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Movement.cs b/Assets/Scripts/Movement.cs index 5c0c921..54a1d05 100644 --- a/Assets/Scripts/Movement.cs +++ b/Assets/Scripts/Movement.cs @@ -12,8 +12,8 @@ public class Movement : MonoBehaviour public float gravity = 9.8f; public PickUpObject throwForce; - public CharacterController controller; - public Vector3 moveDirection = Vector3.zero; + private CharacterController controller; + private Vector3 moveDirection = Vector3.zero; [Header("Keybinds")] public KeyCode forwardsKey = KeyCode.W; @@ -34,46 +34,43 @@ public class Movement : MonoBehaviour float backwards = Convert.ToSingle(Input.GetKey(backwardsKey)); float left = Convert.ToSingle(Input.GetKey(leftKey)); float right = Convert.ToSingle(Input.GetKey(rightKey)); - moveDirection = new Vector3(right - left, 0, forwards - backwards); - - if (!controller.isGrounded) + if (controller.isGrounded) { - moveDirection.y -= gravity * Time.deltaTime; - controller.Move(moveDirection * Time.deltaTime); - return; - } - - throwForce.throwForce = 4.0f; - // Change the direction the player is facing based on the direction they are moving - if (moveDirection != Vector3.zero) - { - Quaternion ToRotation = Quaternion.LookRotation(new Vector3(right - left, 0, forwards - backwards)); - throwForce.throwForce = 8.5f; - transform.rotation = Quaternion.RotateTowards(transform.rotation, ToRotation, turnSpeed * Time.deltaTime); - } - - if (Input.GetKey(sprintKey)) - { - moveDirection *= sprintSpeed; - jumpForce = 4.0f; - if(moveDirection != Vector3.zero) + moveDirection = new Vector3(right - left, 0, forwards - backwards); + // Change the direction the player is facing based on the direction they are moving + if (right - left != 0 || forwards - backwards != 0) { - throwForce.throwForce = 20.0f; + Quaternion ToRotation = Quaternion.LookRotation(moveDirection); + throwForce.throwForce = 8.5f; + transform.rotation = Quaternion.RotateTowards(transform.rotation, ToRotation, turnSpeed * Time.deltaTime); + } + else + { + throwForce.throwForce = 4.0f; } - } - else - { - moveDirection *= speed; - jumpForce = 2.5f; - } - if (Input.GetKeyDown(jumpKey)) - { - moveDirection.y = jumpForce; + if (Input.GetKey(sprintKey)) + { + moveDirection *= sprintSpeed; + jumpForce = 4.0f; + if(right - left != 0 || forwards - backwards != 0) + { + throwForce.throwForce = 20.0f; + } + } + else + { + moveDirection *= speed; + jumpForce = 2.5f; + } + + if (Input.GetKeyDown(jumpKey)) + { + moveDirection.y = jumpForce; + } } moveDirection.y -= gravity * Time.deltaTime; controller.Move(moveDirection * Time.deltaTime); } -} - +} \ No newline at end of file