From a292dd8b9aadf0fafd7340c52bb848cc6b69bc72 Mon Sep 17 00:00:00 2001 From: Gregory Campbell Date: Sat, 14 Dec 2019 23:40:14 -0500 Subject: [PATCH 1/3] Added momentum to player movement and made adjustments to public player variables --- Assets/Scenes/Test Scene.unity | 5 ++- Assets/Scripts/PlayerController.cs | 54 ++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/Assets/Scenes/Test Scene.unity b/Assets/Scenes/Test Scene.unity index 6b9ed7b..5a529d2 100644 --- a/Assets/Scenes/Test Scene.unity +++ b/Assets/Scenes/Test Scene.unity @@ -1284,7 +1284,10 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 83127dd120c01de45954b99cc4d669a5, type: 3} m_Name: m_EditorClassIdentifier: - speed: 20 + speed: 10 + maxSpeed: 25 + acceleration: 5 + friction: 0.1 jumpHeight: 20 gravity: 5 rotationSpeed: 20 diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index fb450db..2b3e12e 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -13,6 +13,9 @@ public class PlayerController : MonoBehaviour CapsuleCollider capsule; public float speed; + public float maxSpeed; + public float acceleration; + public float friction; public float jumpHeight; public float gravity; public float rotationSpeed; @@ -23,7 +26,10 @@ public class PlayerController : MonoBehaviour private float capsuleHeight; private float controllerHeight; private float transformHeight; + private float xVelocity = 0.0f; + private float zVelocity = 0.0f; private Vector3 moveDirection; + private Vector3 velocity; float startTime = 0.0f; float oneSec = 1.0f; @@ -42,13 +48,20 @@ public class PlayerController : MonoBehaviour float yStore = moveDirection.y; //Need to switch to 'raw' when using keyboard - moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal")); + //moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal")); + moveDirection = new Vector3(Input.GetAxis("Horizontal"), moveDirection.y, Input.GetAxis("Vertical")); + moveDirection = transform.TransformDirection(moveDirection); + moveDirection = Vector3.ClampMagnitude(moveDirection, 1.0f); if (jump <= 1) { - moveDirection = moveDirection.normalized * speed; //Remove this line to make running diagonal the fastest standard run + //moveDirection = moveDirection.normalized * speed; //Remove this line to make running diagonal the fastest standard run + velocity.x += moveDirection.x * acceleration; + velocity.z += moveDirection.z * acceleration; } else { - moveDirection = (moveDirection.normalized * speed)/4; //Remove this line to make running diagonal the fastest standard run + //moveDirection = (moveDirection.normalized * speed)/4; //Remove this line to make running diagonal the fastest standard run + velocity.x += (moveDirection.x * acceleration)/4; + velocity.z += (moveDirection.z * acceleration)/4; } moveDirection.y = yStore; @@ -60,9 +73,13 @@ public class PlayerController : MonoBehaviour if(Input.GetKey(KeyCode.LeftShift)) { - moveDirection = (moveDirection.normalized * speed/2); + //moveDirection = (moveDirection.normalized * speed/2); + velocity.x += (moveDirection.x * acceleration)/2; + velocity.z += (moveDirection.x * acceleration)/2; } else { - moveDirection = moveDirection.normalized * speed; + //moveDirection = moveDirection.normalized * speed; + velocity.x += moveDirection.x * acceleration; + velocity.z += moveDirection.z * acceleration; } if(Input.GetKey(KeyCode.LeftControl)) @@ -80,12 +97,16 @@ public class PlayerController : MonoBehaviour if(startTime + oneSec >= Time.time) { - moveDirection = (moveDirection.normalized * speed * 2); - + //moveDirection = (moveDirection.normalized * speed * 2); + velocity.x += (moveDirection.x * acceleration)*2; + velocity.z += (moveDirection.z * acceleration)*2; + } else { - moveDirection = (moveDirection.normalized * speed/4); - + //moveDirection = (moveDirection.normalized * speed/4); + velocity.x += (moveDirection.z * acceleration)/4; + velocity.z += (moveDirection.z * acceleration)/4; + } } else { @@ -95,7 +116,15 @@ public class PlayerController : MonoBehaviour transform.localScale = new Vector3(transform.localScale.x, transformHeight, transform.localScale.z); } - } + velocity.x = Mathf.SmoothDamp(velocity.x, 0.0f, ref xVelocity, friction); + velocity.z = Mathf.SmoothDamp(velocity.z, 0.0f, ref zVelocity, friction); + + } else { + + velocity.x = Mathf.SmoothDamp(velocity.x, 0.0f, ref xVelocity, friction*3); + velocity.z = Mathf.SmoothDamp(velocity.z, 0.0f, ref zVelocity, friction*3); + + } if (Input.GetButtonDown("Jump") && jump <= 1) { @@ -104,9 +133,12 @@ public class PlayerController : MonoBehaviour } moveDirection.y += Physics.gravity.y * gravity * Time.deltaTime; + velocity.y = moveDirection.y; + + velocity = Vector3.ClampMagnitude(velocity, maxSpeed); // Move the controller - characterController.Move(moveDirection * Time.deltaTime); + characterController.Move(velocity * Time.deltaTime); //Move the player in different directions based on camera look direction //Need to switch to 'raw' when using keyboard From 3afdbc4ff0d0e14ead0efcc8adbc8b099b82a936 Mon Sep 17 00:00:00 2001 From: Gregory Campbell Date: Sat, 14 Dec 2019 23:49:23 -0500 Subject: [PATCH 2/3] Fixed issue causing no increased or decreased mmovement based on changing moveset --- Assets/Scripts/PlayerController.cs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 2b3e12e..eef1653 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -23,6 +23,7 @@ public class PlayerController : MonoBehaviour public GameObject playerModel; private int jump = 0; + private float maxSpeedStore; private float capsuleHeight; private float controllerHeight; private float transformHeight; @@ -41,27 +42,31 @@ public class PlayerController : MonoBehaviour transformHeight = transform.localScale.y; controllerHeight = characterController.height; capsuleHeight = capsule.height; + maxSpeedStore = maxSpeed; } void Update() { float yStore = moveDirection.y; + maxSpeed = maxSpeedStore; //Need to switch to 'raw' when using keyboard //moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal")); moveDirection = new Vector3(Input.GetAxis("Horizontal"), moveDirection.y, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection = Vector3.ClampMagnitude(moveDirection, 1.0f); + velocity.x += moveDirection.x * acceleration; + velocity.z += moveDirection.z * acceleration; + if (jump <= 1) { //moveDirection = moveDirection.normalized * speed; //Remove this line to make running diagonal the fastest standard run - velocity.x += moveDirection.x * acceleration; - velocity.z += moveDirection.z * acceleration; + maxSpeed = maxSpeedStore; + } else { //moveDirection = (moveDirection.normalized * speed)/4; //Remove this line to make running diagonal the fastest standard run - velocity.x += (moveDirection.x * acceleration)/4; - velocity.z += (moveDirection.z * acceleration)/4; + maxSpeed = maxSpeedStore/4; } moveDirection.y = yStore; @@ -74,12 +79,10 @@ public class PlayerController : MonoBehaviour if(Input.GetKey(KeyCode.LeftShift)) { //moveDirection = (moveDirection.normalized * speed/2); - velocity.x += (moveDirection.x * acceleration)/2; - velocity.z += (moveDirection.x * acceleration)/2; + maxSpeed = maxSpeedStore/2; } else { //moveDirection = moveDirection.normalized * speed; - velocity.x += moveDirection.x * acceleration; - velocity.z += moveDirection.z * acceleration; + maxSpeed = maxSpeedStore; } if(Input.GetKey(KeyCode.LeftControl)) @@ -98,14 +101,12 @@ public class PlayerController : MonoBehaviour { //moveDirection = (moveDirection.normalized * speed * 2); - velocity.x += (moveDirection.x * acceleration)*2; - velocity.z += (moveDirection.z * acceleration)*2; + maxSpeed = maxSpeedStore*2; } else { //moveDirection = (moveDirection.normalized * speed/4); - velocity.x += (moveDirection.z * acceleration)/4; - velocity.z += (moveDirection.z * acceleration)/4; + maxSpeed = maxSpeedStore/4; } From e6388925922dcf0389b1d1322084793f27a78e4b Mon Sep 17 00:00:00 2001 From: Gregory Campbell Date: Sat, 21 Dec 2019 21:00:36 -0500 Subject: [PATCH 3/3] Got momentum based movement fully functional while fixing a few player controller bugs and preferential tweaks --- Assets/Scenes/Test Scene.unity | 2 +- Assets/Scripts/PlayerController.cs | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Assets/Scenes/Test Scene.unity b/Assets/Scenes/Test Scene.unity index 5a529d2..7b56c45 100644 --- a/Assets/Scenes/Test Scene.unity +++ b/Assets/Scenes/Test Scene.unity @@ -1286,7 +1286,7 @@ MonoBehaviour: m_EditorClassIdentifier: speed: 10 maxSpeed: 25 - acceleration: 5 + acceleration: 3 friction: 0.1 jumpHeight: 20 gravity: 5 diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index eef1653..42080f9 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -66,7 +66,7 @@ public class PlayerController : MonoBehaviour } else { //moveDirection = (moveDirection.normalized * speed)/4; //Remove this line to make running diagonal the fastest standard run - maxSpeed = maxSpeedStore/4; + maxSpeed = maxSpeedStore/2; } moveDirection.y = yStore; @@ -92,10 +92,11 @@ public class PlayerController : MonoBehaviour capsule.height /= 2; transform.localScale = new Vector3(transform.localScale.x, transformHeight/2, transform.localScale.z); - if(Input.GetKeyDown(KeyCode.LeftControl) && characterController.velocity != new Vector3(0, 0, 0)) + if(Input.GetKeyDown(KeyCode.LeftControl) && characterController.velocity != new Vector3(0, 0, 0) + && (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)) { startTime = Time.time; - } + } if(startTime + oneSec >= Time.time) { @@ -117,13 +118,12 @@ public class PlayerController : MonoBehaviour transform.localScale = new Vector3(transform.localScale.x, transformHeight, transform.localScale.z); } - velocity.x = Mathf.SmoothDamp(velocity.x, 0.0f, ref xVelocity, friction); - velocity.z = Mathf.SmoothDamp(velocity.z, 0.0f, ref zVelocity, friction); + } - } else { + if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0){ - velocity.x = Mathf.SmoothDamp(velocity.x, 0.0f, ref xVelocity, friction*3); - velocity.z = Mathf.SmoothDamp(velocity.z, 0.0f, ref zVelocity, friction*3); + velocity.x = Mathf.SmoothDamp(velocity.x, 0.0f, ref xVelocity, friction); + velocity.z = Mathf.SmoothDamp(velocity.z, 0.0f, ref zVelocity, friction); } @@ -134,15 +134,15 @@ public class PlayerController : MonoBehaviour } moveDirection.y += Physics.gravity.y * gravity * Time.deltaTime; - velocity.y = moveDirection.y; velocity = Vector3.ClampMagnitude(velocity, maxSpeed); + velocity.y = moveDirection.y; + // Move the controller characterController.Move(velocity * Time.deltaTime); //Move the player in different directions based on camera look direction - //Need to switch to 'raw' when using keyboard if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) { transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);