Hello all,
I am on implementing a character controller, having a state machine and a problem. When walking while crouching, already in the first frame for whatever reason, the character controller of my player is pushed up which causes the player not being grounded anymore. It happens after the controller.Move.
I took into consideration the skin width, but that’s not the problem. I am having since several weeks, and really cannot find it out. Please help.
That’s the code:
public class CrouchWalkState : ProcanimState
{
public override void OnEnable()
{
data.isCrouching = true;
}
public override void Update()
{
float x = Input.GetKey(KeyCode.D) ? 1 : (Input.GetKey(KeyCode.A) ? -1 : 0);
float z = Input.GetKey(KeyCode.W) ? 1 : (Input.GetKey(KeyCode.S) ? -1 : 0);
// Calculating the horizontal direction, in which the player is moving.
data.horizontalDirection = data.transform.right * x + data.transform.forward * z;
data.speed = 0.75f;
// Calculating the speed, with which the player is moving.
Vector3 movementSpeed = data.horizontalDirection.normalized * data.speed;
// Important for ramp walking, else the player is stuttering
if (data.isGrounded && data.controller.velocity.y < 0.001f)
{
data.verticalVelocity = -5f;
}
else
{
data.verticalVelocity -= data.gravity * Time.deltaTime;
}
movementSpeed.y = data.verticalVelocity;
float posY1 = data.controller.transform.position.y; // debug
data.controller.Move(movementSpeed * Time.deltaTime);
float posY2 = data.controller.transform.position.y; // debug
if(data.controller.transform.position.y > 0.035f) // debug
{
// Breakpoint is here
}
}
}
This is what the debugger shows: Just before the controller.Move the controller position is 0.03, and just after it 0.039, even thought in the controller.Move the controller is pushed down. How can this happen?
Anybody some ideas, why the controller is pushed up? What circumstances can cause, that the controller is behaving this way?
Thank you very much!
Huxi