From 3f3770f74f938e5be13c035832d25b8e5145645c Mon Sep 17 00:00:00 2001 From: Glence Date: Tue, 15 Nov 2022 18:52:46 +0800 Subject: [PATCH] now hard sets the vel instead of adding force --- Assets/Scripts/SC_PlayerController.cs | 48 +++++++++------------------ 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/Assets/Scripts/SC_PlayerController.cs b/Assets/Scripts/SC_PlayerController.cs index bc8467c2..8ca17674 100644 --- a/Assets/Scripts/SC_PlayerController.cs +++ b/Assets/Scripts/SC_PlayerController.cs @@ -41,7 +41,7 @@ public class PlayerController : Script [Tooltip("Max vel for walking")] public float maxMoveVel = 2.0f; [Tooltip("how much force is apply for walking")] - public float moveForce = 50.0f; + public float moveForce = 2.0f; [Tooltip("increase the moveForce and maxMoveVel by its amt")] public float sprintMultiplier = 2.0f; @@ -137,11 +137,10 @@ public class PlayerController : Script protected override void fixedUpdate() { //Rotation(); - if (rb) - Debug.Log($"Before x: {rb.LinearVelocity.x} z:{rb.LinearVelocity.z}"); MoveKey(); + //to be change if (rb) rb.AddForce(new Vector3(-rb.GetForce().x, -rb.GetForce().y, -rb.GetForce().z)); @@ -149,28 +148,12 @@ public class PlayerController : Script Sprint(); Jump(); Gravity(); - - if (rb) - Debug.Log($"After x: {rb.LinearVelocity.x} z:{rb.LinearVelocity.z}"); + //Debug.Log($"X: {rb.LinearVelocity.x}" + $" z: {rb.LinearVelocity.z}"); } private void MoveKey() { - /* if (Input.GetKey(Input.KeyCode.A)) - xAxisMove = -1; - else if (Input.GetKey(Input.KeyCode.D)) - xAxisMove = 1; - else - xAxisMove = 0; - - if (Input.GetKey(Input.KeyCode.W)) - zAxisMove = -1; - else if (Input.GetKey(Input.KeyCode.S)) - zAxisMove = 1; - else - zAxisMove = 0;*/ - xAxisMove = 0; zAxisMove = 0; @@ -240,22 +223,23 @@ public class PlayerController : Script { if (rb != null) { - rb.AddForce(new Vector3(axisMove.x, 0.0f,axisMove.y) * moveForce); - + //rb.AddForce(new Vector3(axisMove.x, 0.0f,axisMove.y) * moveForce); + rb.LinearVelocity = new Vector3(axisMove.x * moveForce, rb.LinearVelocity.y, axisMove.y * moveForce); + if (isMoveKeyPress) { - if (rb.LinearVelocity.x > maxMoveVel || rb.LinearVelocity.x < -maxMoveVel) + if (rb) { - Vector3 v = rb.LinearVelocity; - v.x = System.Math.Clamp(v.x, -maxMoveVel, maxMoveVel); - rb.LinearVelocity = v; - } - if (rb.LinearVelocity.z > maxMoveVel || rb.LinearVelocity.z < -maxMoveVel) - { - Vector3 v = rb.LinearVelocity; - v.z = System.Math.Clamp(v.z, -maxMoveVel, maxMoveVel); - rb.LinearVelocity = v; + Vector3 velNor = rb.LinearVelocity; + velNor.y = 0.0f; + if (velNor.GetMagnitude() > maxMoveVel) + { + velNor.Normalise(); + velNor *= maxMoveVel; + rb.LinearVelocity = new Vector3(velNor.x, rb.LinearVelocity.y, velNor.z); + } } + } } }