Reworked physics so speed is much closer for originally intended (might want to tweak a bit more)

This commit is contained in:
Gregory Campbell
2020-02-11 18:37:48 -05:00
parent 4d18b43bcf
commit 3fbfe098f4
+61 -40
View File
@@ -48,6 +48,8 @@ public class PlayerController : MonoBehaviour
private Vector3 velocity; private Vector3 velocity;
private Vector3 gravityVelocity; private Vector3 gravityVelocity;
bool jumpPress = false;
bool grounded = true;
float slideTime = 0.0f; float slideTime = 0.0f;
float wallRunTime = 0.0f; float wallRunTime = 0.0f;
public float respawnTime = 0.0f; public float respawnTime = 0.0f;
@@ -75,7 +77,7 @@ public class PlayerController : MonoBehaviour
mr = playerSkin.GetComponent<MeshRenderer>(); mr = playerSkin.GetComponent<MeshRenderer>();
mr.material = characterSkins[characterSelect]; mr.material = characterSkins[characterSelect];
Application.targetFrameRate = 60; Application.targetFrameRate = 15;
} }
void OnControllerColliderHit(ControllerColliderHit hit) void OnControllerColliderHit(ControllerColliderHit hit)
@@ -152,14 +154,6 @@ public class PlayerController : MonoBehaviour
void Update() void Update()
{ {
float yStore = moveDirection.y;
moveDirection = new Vector3(Input.GetAxis("Horizontal"), moveDirection.y, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection = Vector3.ClampMagnitude(moveDirection, 1.0f);
moveDirection.y = yStore;
moveDirection.y += Physics.gravity.y * gravity * Time.deltaTime;
if (jump >= 2) if (jump >= 2)
{ {
if(maxSpeed > maxSpeedStore/1.5f) if(maxSpeed > maxSpeedStore/1.5f)
@@ -174,32 +168,6 @@ public class PlayerController : MonoBehaviour
wall = 0; wall = 0;
} }
if (wallRunning)
{
if(maxSpeed < maxSpeedStore*1.5f)
{
maxSpeed += acceleration;
}
jump = 0;
if(wall == 1)
{
wallRunTime = Time.time;
}
if(wallRunTime + oneSec < Time.time)
{
moveDirection.y += Physics.gravity.y * (gravity/8) * Time.deltaTime;
} else {
moveDirection.y = 0.0f;
}
}
if(transform.position.y < -20.0f) if(transform.position.y < -20.0f)
{ {
@@ -219,6 +187,7 @@ public class PlayerController : MonoBehaviour
if (characterController.isGrounded) if (characterController.isGrounded)
{ {
grounded = true;
diving = false; diving = false;
dive = 0; dive = 0;
jump = 0; jump = 0;
@@ -273,7 +242,9 @@ public class PlayerController : MonoBehaviour
} else { } else {
if(maxSpeed > maxSpeedStore/4) friction = 0f;
if(maxSpeed > 1)
{ {
maxSpeed -= acceleration*2; maxSpeed -= acceleration*2;
} }
@@ -293,6 +264,7 @@ public class PlayerController : MonoBehaviour
slideTime = Time.time; slideTime = Time.time;
sliding = false; sliding = false;
grounded = false;
if((Input.GetButton("Crouch") || Input.GetAxis("Crouch") == 1.0f) && !PauseMenu.GameIsPaused) if((Input.GetButton("Crouch") || Input.GetAxis("Crouch") == 1.0f) && !PauseMenu.GameIsPaused)
{ {
@@ -314,16 +286,14 @@ public class PlayerController : MonoBehaviour
if(diving && moveDirection.y > 0.75 && (Input.GetButtonDown("Crouch") || Input.GetAxis("Crouch") == 1.0f )) if(diving && moveDirection.y > 0.75 && (Input.GetButtonDown("Crouch") || Input.GetAxis("Crouch") == 1.0f ))
{ {
dive++; dive++;
if(dive == 1){
maxSpeed += 10;
}
} }
} }
if (Input.GetButtonDown("Jump") && jump <= 1) if (Input.GetButtonDown("Jump") && jump <= 1)
{ {
moveDirection.y = jumpHeight; jumpPress = true;
jump++; jump++;
jumpNoise.Play(); jumpNoise.Play();
@@ -362,9 +332,54 @@ public class PlayerController : MonoBehaviour
void FixedUpdate() void FixedUpdate()
{ {
float yStore = moveDirection.y;
moveDirection = new Vector3(Input.GetAxis("Horizontal"), moveDirection.y, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection = Vector3.ClampMagnitude(moveDirection, 1.0f);
moveDirection.y = yStore;
if (wallRunning)
{
if(maxSpeed < maxSpeedStore*1.5f)
{
maxSpeed += acceleration;
}
jump = 0;
if(wall == 1)
{
wallRunTime = Time.time;
}
if(wallRunTime + oneSec < Time.time)
{
moveDirection.y += Physics.gravity.y * (gravity/8) * Time.deltaTime;
} else {
moveDirection.y = 0.0f;
}
}
if(dive == 1){
maxSpeed += 10;
}
moveDirection.y += Physics.gravity.y * gravity * Time.deltaTime;
velocity.x += moveDirection.x; velocity.x += moveDirection.x;
velocity.z += moveDirection.z; velocity.z += moveDirection.z;
if((!grounded || sliding) && !wallRunning)
{
friction = 1.75f;
} else {
friction = 1f;
}
//Remove or "lower" friction to add an 'ice' effect //Remove or "lower" friction to add an 'ice' effect
velocity.x = Mathf.SmoothDamp(velocity.x, 0.0f, ref xVelocity, friction); velocity.x = Mathf.SmoothDamp(velocity.x, 0.0f, ref xVelocity, friction);
velocity.z = Mathf.SmoothDamp(velocity.z, 0.0f, ref zVelocity, friction); velocity.z = Mathf.SmoothDamp(velocity.z, 0.0f, ref zVelocity, friction);
@@ -376,6 +391,12 @@ public class PlayerController : MonoBehaviour
velocity = Vector3.ClampMagnitude(velocity, maxSpeed); velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
if(jumpPress)
{
moveDirection.y = jumpHeight;
jumpPress = false;
}
velocity.y = moveDirection.y; velocity.y = moveDirection.y;
// Move the controller // Move the controller