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")]
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,23 +223,24 @@ 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 velNor = rb.LinearVelocity;
velNor.y = 0.0f;
if (velNor.GetMagnitude() > maxMoveVel)
{
Vector3 v = rb.LinearVelocity;
v.z = System.Math.Clamp(v.z, -maxMoveVel, maxMoveVel);
rb.LinearVelocity = v;
velNor.Normalise();
velNor *= maxMoveVel;
rb.LinearVelocity = new Vector3(velNor.x, rb.LinearVelocity.y, velNor.z);
}
}
}
}
}