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.
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:
Anybody some ideas, why the controller is pushed up?
Thank you
Huxi