From e3064f0823a5141b09cab4a589ef391a628aa313 Mon Sep 17 00:00:00 2001 From: DerTyp187 Date: Tue, 21 Sep 2021 18:34:02 +0200 Subject: [PATCH] basic air control and sprinting --- Assets/Scenes/SampleScene.unity | 5 ++- Assets/Scripts/PlayerMovement.cs | 64 +++++++++++++++++++++------ ProjectSettings/InputManager.asset | 16 +++++++ ProjectSettings/ProjectSettings.asset | 3 +- 4 files changed, 72 insertions(+), 16 deletions(-) diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 3419fd6..24b4ef7 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -733,8 +733,11 @@ MonoBehaviour: m_EditorClassIdentifier: controller: {fileID: 1023538875} speed: 12 + airSpeed: 0.7 + sprintSpeed: 1.8 + sprintAirSpeed: 1.2 gravity: -19.62 - jumpHeight: 3 + jumpHeight: 4 groundCheck: {fileID: 1403327789} groundDistance: 0.4 groundMask: diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs index e5afbf6..27602c2 100644 --- a/Assets/Scripts/PlayerMovement.cs +++ b/Assets/Scripts/PlayerMovement.cs @@ -6,7 +6,11 @@ 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 sprintSpeed = 1.8f; + [SerializeField] private float sprintAirSpeed = 1.2f; [SerializeField] private float gravity = -9.81f; [SerializeField] private float jumpHeight = 3f; @@ -16,6 +20,7 @@ public class PlayerMovement : MonoBehaviour private Vector3 velocity; private bool isGrounded; + private bool isSprinting; void Start() { @@ -24,7 +29,8 @@ public class PlayerMovement : MonoBehaviour 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); @@ -34,30 +40,60 @@ public class PlayerMovement : MonoBehaviour { 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 = transform.right * x + transform.forward * z; + 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")) + { + //Kommt mit character model und animations + } + 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); - //JUMP - if(Input.GetButtonDown("Jump") && isGrounded) - { - velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); - } - //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 - } - - void Jump() - { } } diff --git a/ProjectSettings/InputManager.asset b/ProjectSettings/InputManager.asset index 17c8f53..73ab00b 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 ctrl + altNegativeButton: + altPositiveButton: + gravity: 1000 + dead: 0.001 + sensitivity: 1000 + snap: 0 + invert: 0 + type: 0 + axis: 0 + joyNum: 0 diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 387057b..8b5fce3 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -152,7 +152,8 @@ PlayerSettings: resolutionScalingMode: 0 androidSupportedAspectRatio: 1 androidMaxAspectRatio: 2.1 - applicationIdentifier: {} + applicationIdentifier: + Standalone: com.DefaultCompany.FirstPersonRME buildNumber: Standalone: 0 iPhone: 0