I want to make my character unable to change his movement direction while not connected to the ground (or a wall for that matter). In old unity I could simply disable the .canControl of CharacterMotor and enable it again when the character comes close enough to a surface.
Is it possible to achieve the same effect in the new first person controller? I tried to simply equate the input on both axes to 0, but that stops character dead in his tracks the moment he jumps.
Simply put: I want to disable control but keep the momentum and acceleration. Is it possible to achieve that without writing a completely new first person controller?
I modified standard FPS controller FixedUpdate a little bit to limit air control.
private Vector3 mLastDesiredMove;
private float mLastSpeed;
private void FixedUpdate()
{
float speed;
GetInput(out speed);
// always move along the camera forward as it is the direction that it being aimed at
Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;
// get a normal for the surface that is being touched to move along it
RaycastHit hitInfo;
Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
m_CharacterController.height/2f, Physics.AllLayers, QueryTriggerInteraction.Ignore);
desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
// On air limit
if (m_CharacterController.isGrounded)
{
mLastDesiredMove = desiredMove;
mLastSpeed = speed;
}
else
{
desiredMove = mLastDesiredMove + desiredMove * 0.25f;
speed = mLastSpeed;
}
m_MoveDir.x = desiredMove.x*speed;
m_MoveDir.z = desiredMove.z*speed;
if (m_CharacterController.isGrounded)
{
m_MoveDir.y = -m_StickToGroundForce;
if (m_Jump)
{
m_MoveDir.y = m_JumpSpeed;
PlayJumpSound();
m_Jump = false;
m_Jumping = true;
}
}
else
{
m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
}
m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
ProgressStepCycle(speed);
UpdateCameraPosition(speed);
m_MouseLook.UpdateCursorLock();
}