From 573b4815eae1463c29401a164a47f69378edb901 Mon Sep 17 00:00:00 2001 From: Gregory Campbell Date: Sat, 4 Jan 2020 15:02:05 -0500 Subject: [PATCH 1/3] Havent made any real progress, stopping and saving this now to work on other things --- Assets/Scripts/PlayerController.cs | 61 ++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 1ff4613..44c146f 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -26,6 +26,7 @@ public class PlayerController : MonoBehaviour private int jump = 0; private int wall = 0; private float maxSpeedStore; + private float accelerationStore; private float capsuleHeight; private float controllerHeight; private float transformHeight; @@ -45,6 +46,7 @@ public class PlayerController : MonoBehaviour controllerHeight = characterController.height; capsuleHeight = capsule.height; maxSpeedStore = maxSpeed; + accelerationStore = acceleration; } void OnControllerColliderHit(ControllerColliderHit hit) @@ -63,15 +65,13 @@ public class PlayerController : MonoBehaviour float yStore = moveDirection.y; maxSpeed = maxSpeedStore; + acceleration = accelerationStore; //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 @@ -79,7 +79,8 @@ public class PlayerController : MonoBehaviour } else { //moveDirection = (moveDirection.normalized * speed)/4; //Remove this line to make running diagonal the fastest standard run - maxSpeed = maxSpeedStore/2; + //maxSpeed = maxSpeedStore/1.75f; + acceleration = acceleration/2; } moveDirection.y = yStore; @@ -92,10 +93,12 @@ public class PlayerController : MonoBehaviour if(Input.GetKey(KeyCode.LeftShift)) { //moveDirection = (moveDirection.normalized * speed/2); - maxSpeed = maxSpeedStore/2; + //maxSpeed = maxSpeedStore/2; + acceleration = acceleration/2; } else { //moveDirection = moveDirection.normalized * speed; - maxSpeed = maxSpeedStore; + //maxSpeed = maxSpeedStore; + acceleration = accelerationStore; } if(Input.GetKey(KeyCode.LeftControl)) @@ -115,17 +118,20 @@ public class PlayerController : MonoBehaviour { //moveDirection = (moveDirection.normalized * speed * 2); - maxSpeed = maxSpeedStore*2; + //maxSpeed = maxSpeedStore*2; + acceleration = acceleration*2; } else { //moveDirection = (moveDirection.normalized * speed/4); - maxSpeed = maxSpeedStore/4; + //maxSpeed = maxSpeedStore/4; + acceleration = acceleration/4; } } else { - moveDirection = moveDirection.normalized * speed; + //moveDirection = moveDirection.normalized * speed; + moveDirection = moveDirection.normalized; characterController.height = controllerHeight; capsule.height = capsuleHeight; transform.localScale = new Vector3(transform.localScale.x, transformHeight, transform.localScale.z); @@ -133,13 +139,6 @@ public class PlayerController : MonoBehaviour } - if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0){ - - velocity.x = Mathf.SmoothDamp(velocity.x, 0.0f, ref xVelocity, friction); - velocity.z = Mathf.SmoothDamp(velocity.z, 0.0f, ref zVelocity, friction); - - } - if (Input.GetButtonDown("Jump") && jump <= 1) { moveDirection.y = jumpHeight; @@ -156,7 +155,8 @@ public class PlayerController : MonoBehaviour if (wallRunning) { - maxSpeed = maxSpeedStore*1.5f; + //maxSpeed = maxSpeedStore*1.5f; + acceleration = acceleration*1.5f; jump = 0; if(wall == 1) @@ -169,12 +169,35 @@ public class PlayerController : MonoBehaviour moveDirection.y += Physics.gravity.y * (gravity/8) * Time.deltaTime; //wallRunning = false; - maxSpeed = maxSpeedStore; + //maxSpeed = maxSpeedStore; } else { moveDirection.y = 0.0f; } - } + } + + velocity.x += moveDirection.x * acceleration; + velocity.z += moveDirection.z * acceleration; + + //if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0){ + + //Remove or lower friction to add an 'ice' effect + //velocity.x = Mathf.SmoothDamp(velocity.x, 0.0f, ref xVelocity, friction); + //velocity.z = Mathf.SmoothDamp(velocity.z, 0.0f, ref zVelocity, friction); + + //} + + if(velocity.x > 0.0f){ + velocity.x -= 0.5f; + } else if(velocity.x < 0.0f){ + velocity.x += 0.5f; + } + + if(velocity.z > 0.0f){ + velocity.z -= 0.5f; + } else if(velocity.z < 0.0f){ + velocity.z += 0.5f; + } velocity = Vector3.ClampMagnitude(velocity, maxSpeed); From 386b51589ef7c9f64be328517d18d96a0ed6fc30 Mon Sep 17 00:00:00 2001 From: Gregory Campbell Date: Sat, 4 Jan 2020 15:52:45 -0500 Subject: [PATCH 2/3] Got a fully functional implementation of acceleration/decceleration that meshes well across all inputs. Will do further testing/tinkering before merging --- Assets/Scripts/PlayerController.cs | 94 +++++++++++++++--------------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 44c146f..678ff90 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -26,6 +26,7 @@ public class PlayerController : MonoBehaviour private int jump = 0; private int wall = 0; private float maxSpeedStore; + private float maxSpeedCap = 100; private float accelerationStore; private float capsuleHeight; private float controllerHeight; @@ -64,23 +65,16 @@ public class PlayerController : MonoBehaviour { float yStore = moveDirection.y; - maxSpeed = maxSpeedStore; - acceleration = accelerationStore; - //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); - if (jump <= 1) + if (jump > 2) { - //moveDirection = moveDirection.normalized * speed; //Remove this line to make running diagonal the fastest standard run - //maxSpeed = maxSpeedStore; - - } else { - //moveDirection = (moveDirection.normalized * speed)/4; //Remove this line to make running diagonal the fastest standard run - //maxSpeed = maxSpeedStore/1.75f; - acceleration = acceleration/2; + if(maxSpeed > maxSpeedStore/3) + { + maxSpeed -= acceleration*3; + } } moveDirection.y = yStore; @@ -92,13 +86,18 @@ public class PlayerController : MonoBehaviour if(Input.GetKey(KeyCode.LeftShift)) { - //moveDirection = (moveDirection.normalized * speed/2); - //maxSpeed = maxSpeedStore/2; - acceleration = acceleration/2; - } else { - //moveDirection = moveDirection.normalized * speed; - //maxSpeed = maxSpeedStore; - acceleration = accelerationStore; + if(maxSpeed > maxSpeedStore/2) + { + maxSpeed -= acceleration; + } + } + + if(Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.LeftControl)) + { + while(maxSpeed < maxSpeedStore) + { + maxSpeed += acceleration; + } } if(Input.GetKey(KeyCode.LeftControl)) @@ -117,24 +116,27 @@ public class PlayerController : MonoBehaviour if(startTime + oneSec >= Time.time) { - //moveDirection = (moveDirection.normalized * speed * 2); - //maxSpeed = maxSpeedStore*2; - acceleration = acceleration*2; + if(maxSpeed < maxSpeedStore*2) + { + maxSpeed += acceleration; + } } else { - //moveDirection = (moveDirection.normalized * speed/4); - //maxSpeed = maxSpeedStore/4; - acceleration = acceleration/4; + if(maxSpeed > maxSpeedStore/4) + { + maxSpeed -= acceleration; + } } } else { - //moveDirection = moveDirection.normalized * speed; - moveDirection = moveDirection.normalized; + + //maxSpeed = maxSpeedStore; characterController.height = controllerHeight; capsule.height = capsuleHeight; transform.localScale = new Vector3(transform.localScale.x, transformHeight, transform.localScale.z); + } } @@ -155,8 +157,12 @@ public class PlayerController : MonoBehaviour if (wallRunning) { - //maxSpeed = maxSpeedStore*1.5f; - acceleration = acceleration*1.5f; + + if(maxSpeed < maxSpeedStore*1.5f) + { + maxSpeed += acceleration; + } + jump = 0; if(wall == 1) @@ -168,35 +174,31 @@ public class PlayerController : MonoBehaviour { moveDirection.y += Physics.gravity.y * (gravity/8) * Time.deltaTime; - //wallRunning = false; - //maxSpeed = maxSpeedStore; } else { moveDirection.y = 0.0f; } } - velocity.x += moveDirection.x * acceleration; - velocity.z += moveDirection.z * acceleration; + velocity.x += moveDirection.x; + velocity.z += moveDirection.z; - //if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0){ + if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0){ - //Remove or lower friction to add an 'ice' effect - //velocity.x = Mathf.SmoothDamp(velocity.x, 0.0f, ref xVelocity, friction); - //velocity.z = Mathf.SmoothDamp(velocity.z, 0.0f, ref zVelocity, friction); + //Remove or "lower" friction to add an 'ice' effect + velocity.x = Mathf.SmoothDamp(velocity.x, 0.0f, ref xVelocity, friction); + velocity.z = Mathf.SmoothDamp(velocity.z, 0.0f, ref zVelocity, friction); - //} + } - if(velocity.x > 0.0f){ - velocity.x -= 0.5f; - } else if(velocity.x < 0.0f){ - velocity.x += 0.5f; + if(maxSpeed > maxSpeedCap) + { + maxSpeed = maxSpeedCap; } - if(velocity.z > 0.0f){ - velocity.z -= 0.5f; - } else if(velocity.z < 0.0f){ - velocity.z += 0.5f; + if(characterController.velocity == new Vector3(0, 0, 0)) + { + maxSpeed = maxSpeedStore; } velocity = Vector3.ClampMagnitude(velocity, maxSpeed); From 996e1e3b208197d5e07fdbcf44adea9cacd81914 Mon Sep 17 00:00:00 2001 From: Gregory Campbell Date: Sat, 4 Jan 2020 16:15:15 -0500 Subject: [PATCH 3/3] Fully functional and satisfactory acc/decc in movement (although a little slippery, might require more tweakign later on) --- Assets/Scripts/PlayerController.cs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 678ff90..15ad091 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -12,7 +12,6 @@ public class PlayerController : MonoBehaviour CharacterController characterController; CapsuleCollider capsule; - public float speed; public float maxSpeed; public float acceleration; public float friction; @@ -69,11 +68,11 @@ public class PlayerController : MonoBehaviour moveDirection = transform.TransformDirection(moveDirection); moveDirection = Vector3.ClampMagnitude(moveDirection, 1.0f); - if (jump > 2) + if (jump >= 2) { - if(maxSpeed > maxSpeedStore/3) + if(maxSpeed > maxSpeedStore/1.75f) { - maxSpeed -= acceleration*3; + maxSpeed -= acceleration; } } @@ -84,20 +83,22 @@ public class PlayerController : MonoBehaviour jump = 0; moveDirection.y = 0.0f; + if(maxSpeed > maxSpeedStore) + { + maxSpeed -= acceleration/10; + } + + if(maxSpeed < maxSpeedStore && !Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.LeftControl)) + { + maxSpeed += acceleration; + } + if(Input.GetKey(KeyCode.LeftShift)) { if(maxSpeed > maxSpeedStore/2) { maxSpeed -= acceleration; } - } - - if(Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.LeftControl)) - { - while(maxSpeed < maxSpeedStore) - { - maxSpeed += acceleration; - } } if(Input.GetKey(KeyCode.LeftControl)) @@ -118,7 +119,7 @@ public class PlayerController : MonoBehaviour if(maxSpeed < maxSpeedStore*2) { - maxSpeed += acceleration; + maxSpeed += acceleration*2; } } else { @@ -132,7 +133,6 @@ public class PlayerController : MonoBehaviour } else { - //maxSpeed = maxSpeedStore; characterController.height = controllerHeight; capsule.height = capsuleHeight; transform.localScale = new Vector3(transform.localScale.x, transformHeight, transform.localScale.z);