- Home /
Disable movement control during jump
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?
@$$anonymous$$akiavel I used a Bool variable that would go like this: If you were not in contact with the ground, go to false. if in contact go to true.
And then in Update/FixedUpdate class if the boolean is true, do everything below (all moving controls).
BUT: The true/false method works, I can put the bool on true, $$anonymous$$oves, and false, Cant controll my object. But my Collision check if im in contact with ground. Doesnt work. Tell me if you got a way to check for collision with ground. Because it doesnt switch automatically for me. (Probably something I did wrong)
Answer by fabian0010k · May 26, 2016 at 09:44 PM
Found a solution (if you are using the FPS controller): In your FirstPersonController change line 219 from
m_Input = new Vector2(horizontal, vertical);
to
if(!m_Jumping)
m_Input = new Vector2(horizontal, vertical);
Answer by ttesla · Apr 14, 2019 at 05:52 PM
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();
}
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
About in air control 2 Answers
Trying to get accelerated jump speed. 2 Answers
In air movement troubles. 1 Answer
tilt control for jump 0 Answers