From bda6ed09ce9001f0411fde5159a9628536bb41da Mon Sep 17 00:00:00 2001 From: Ken Schaefer Date: Fri, 17 Jan 2020 11:09:58 -0600 Subject: [PATCH] Working on running --- .../Scripts/PlayerMovement.cs | 80 +++++++++++-------- .../Variables/PlayerRunSpeed.asset | 16 ++++ .../Variables/PlayerRunSpeed.asset.meta | 8 ++ ...layerSpeed.asset => PlayerWalkSpeed.asset} | 2 +- ....asset.meta => PlayerWalkSpeed.asset.meta} | 0 5 files changed, 70 insertions(+), 36 deletions(-) create mode 100644 Assets/FirstPersonController/Variables/PlayerRunSpeed.asset create mode 100644 Assets/FirstPersonController/Variables/PlayerRunSpeed.asset.meta rename Assets/FirstPersonController/Variables/{PlayerSpeed.asset => PlayerWalkSpeed.asset} (94%) rename Assets/FirstPersonController/Variables/{PlayerSpeed.asset.meta => PlayerWalkSpeed.asset.meta} (100%) diff --git a/Assets/FirstPersonController/Scripts/PlayerMovement.cs b/Assets/FirstPersonController/Scripts/PlayerMovement.cs index 8e5c016..1f0dd68 100644 --- a/Assets/FirstPersonController/Scripts/PlayerMovement.cs +++ b/Assets/FirstPersonController/Scripts/PlayerMovement.cs @@ -19,47 +19,57 @@ using UnityEngine; public class PlayerMovement : MonoBehaviour { - [Header("Character")] - [SerializeField] private CharacterController controller; + [Header("Character")] + [SerializeField] private CharacterController controller; - [Header("Attributes")] - [SerializeField] private FloatReference playerSpeed; - [SerializeField] private FloatReference jumpHeight; + [Header("Attributes")] + [SerializeField] private FloatReference playerWalkSpeed; + [SerializeField] private FloatReference playerRunSpeed; + [SerializeField] private FloatReference jumpHeight; - [Header("Environment")] - [SerializeField] private FloatReference gravity; - private Vector3 velocity; + [Header("Environment")] + [SerializeField] private FloatReference gravity; + private Vector3 velocity; - [Header("Ground")] - [SerializeField] private Transform groundCheck; - [SerializeField] private float groundDistance = 0.4f; - [SerializeField] private LayerMask groundMask; - private bool isGrounded; + [Header("Ground")] + [SerializeField] private Transform groundCheck; + [SerializeField] private float groundDistance = 0.4f; + [SerializeField] private LayerMask groundMask; + private bool isGrounded; - void Update() - { - // Check if the player is on the ground and reset gravity - isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); - if (isGrounded && velocity.y < 0) - { - velocity.y = -2f; - } + void Update() + { + // Check if the player is on the ground and reset gravity + isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); + if (isGrounded && velocity.y < 0) + { + velocity.y = -2f; + } - // Implement movement - float x = Input.GetAxis("Horizontal"); - float z = Input.GetAxis("Vertical"); + // Implement movement + float x = Input.GetAxis("Horizontal"); + float z = Input.GetAxis("Vertical"); - Vector3 move = transform.right * x + transform.forward * z; - controller.Move(move * playerSpeed.Value * Time.deltaTime); + Vector3 move = transform.right * x + transform.forward * z; + if (Input.GetKeyDown(KeyCode.LeftShift) && isGrounded) + { + Debug.Log("Running"); + controller.Move(move * playerRunSpeed * Time.deltaTime); + } + else + { + Debug.Log("Walking"); + controller.Move(move * playerWalkSpeed.Value * Time.deltaTime); + } - // Implement jumping - if(Input.GetButtonDown("Jump") && isGrounded) - { - velocity.y = Mathf.Sqrt(jumpHeight.Value * -2f * gravity.Value); - } + // Implement jumping + if (Input.GetButtonDown("Jump") && isGrounded) + { + velocity.y = Mathf.Sqrt(jumpHeight.Value * -2f * gravity.Value); + } - // Implement gravity - velocity.y += gravity.Value * Time.deltaTime; - controller.Move(velocity * Time.deltaTime); - } + // Implement gravity + velocity.y += gravity.Value * Time.deltaTime; + controller.Move(velocity * Time.deltaTime); + } } diff --git a/Assets/FirstPersonController/Variables/PlayerRunSpeed.asset b/Assets/FirstPersonController/Variables/PlayerRunSpeed.asset new file mode 100644 index 0000000..0f9acec --- /dev/null +++ b/Assets/FirstPersonController/Variables/PlayerRunSpeed.asset @@ -0,0 +1,16 @@ +%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: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} + m_Name: PlayerRunSpeed + m_EditorClassIdentifier: + DeveloperDescription: + Value: 36 diff --git a/Assets/FirstPersonController/Variables/PlayerRunSpeed.asset.meta b/Assets/FirstPersonController/Variables/PlayerRunSpeed.asset.meta new file mode 100644 index 0000000..5d357bf --- /dev/null +++ b/Assets/FirstPersonController/Variables/PlayerRunSpeed.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e96483bba2736bb499237d1aa26f580a +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/FirstPersonController/Variables/PlayerSpeed.asset b/Assets/FirstPersonController/Variables/PlayerWalkSpeed.asset similarity index 94% rename from Assets/FirstPersonController/Variables/PlayerSpeed.asset rename to Assets/FirstPersonController/Variables/PlayerWalkSpeed.asset index a854056..dde503e 100644 --- a/Assets/FirstPersonController/Variables/PlayerSpeed.asset +++ b/Assets/FirstPersonController/Variables/PlayerWalkSpeed.asset @@ -10,7 +10,7 @@ MonoBehaviour: m_Enabled: 1 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} - m_Name: PlayerSpeed + m_Name: PlayerWalkSpeed m_EditorClassIdentifier: DeveloperDescription: Value: 12 diff --git a/Assets/FirstPersonController/Variables/PlayerSpeed.asset.meta b/Assets/FirstPersonController/Variables/PlayerWalkSpeed.asset.meta similarity index 100% rename from Assets/FirstPersonController/Variables/PlayerSpeed.asset.meta rename to Assets/FirstPersonController/Variables/PlayerWalkSpeed.asset.meta