now hard sets the vel instead of adding force

This commit is contained in:
Glence 2022-11-15 18:52:46 +08:00
parent 37bbf22779
commit 3f3770f74f
1 changed files with 16 additions and 32 deletions

View File

@ -41,7 +41,7 @@ public class PlayerController : Script
[Tooltip("Max vel for walking")] [Tooltip("Max vel for walking")]
public float maxMoveVel = 2.0f; public float maxMoveVel = 2.0f;
[Tooltip("how much force is apply for walking")] [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")] [Tooltip("increase the moveForce and maxMoveVel by its amt")]
public float sprintMultiplier = 2.0f; public float sprintMultiplier = 2.0f;
@ -137,11 +137,10 @@ public class PlayerController : Script
protected override void fixedUpdate() protected override void fixedUpdate()
{ {
//Rotation(); //Rotation();
if (rb)
Debug.Log($"Before x: {rb.LinearVelocity.x} z:{rb.LinearVelocity.z}");
MoveKey(); MoveKey();
//to be change
if (rb) if (rb)
rb.AddForce(new Vector3(-rb.GetForce().x, -rb.GetForce().y, -rb.GetForce().z)); rb.AddForce(new Vector3(-rb.GetForce().x, -rb.GetForce().y, -rb.GetForce().z));
@ -149,28 +148,12 @@ public class PlayerController : Script
Sprint(); Sprint();
Jump(); Jump();
Gravity(); Gravity();
//Debug.Log($"X: {rb.LinearVelocity.x}" + $" z: {rb.LinearVelocity.z}");
if (rb)
Debug.Log($"After x: {rb.LinearVelocity.x} z:{rb.LinearVelocity.z}");
} }
private void MoveKey() 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; xAxisMove = 0;
zAxisMove = 0; zAxisMove = 0;
@ -240,22 +223,23 @@ public class PlayerController : Script
{ {
if (rb != null) 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 (isMoveKeyPress)
{ {
if (rb.LinearVelocity.x > maxMoveVel || rb.LinearVelocity.x < -maxMoveVel) if (rb)
{ {
Vector3 v = rb.LinearVelocity; Vector3 velNor = rb.LinearVelocity;
v.x = System.Math.Clamp(v.x, -maxMoveVel, maxMoveVel); velNor.y = 0.0f;
rb.LinearVelocity = v; if (velNor.GetMagnitude() > maxMoveVel)
} {
if (rb.LinearVelocity.z > maxMoveVel || rb.LinearVelocity.z < -maxMoveVel) velNor.Normalise();
{ velNor *= maxMoveVel;
Vector3 v = rb.LinearVelocity; rb.LinearVelocity = new Vector3(velNor.x, rb.LinearVelocity.y, velNor.z);
v.z = System.Math.Clamp(v.z, -maxMoveVel, maxMoveVel); }
rb.LinearVelocity = v;
} }
} }
} }
} }