Question by
KraljVipex · Jul 27, 2017 at 03:51 PM ·
fpsfps controllerfps tutorialfpswalkerfps-controller
Double jump FPS Controller
If in air i press space,when i land jump again.Or if i press space 2 times .How to fix it?
Comment
Answer by Unitykullanici · Aug 26, 2017 at 10:12 AM
Change it on the FPSController.cs!
void Update () {
...
if (!m_Jump) {
m_Jump = CrossPlatformInputManager.GetButtonDown ("Jump");
}
...
}
to
if (!m_Jump) {
if (m_CharacterController.isGrounded) {
m_Jump = CrossPlatformInputManager.GetButtonDown ("Jump");
}
}
That just makes the character jump normally again(I tried it). I'll post my script here because it has the same bug, you can check it if you want.
private bool doublejumppossible;
and
if (!m_Jump || doublejumppossible == true)
{
if (doublejumppossible == true){
doublejumppossible = false;
}
m_Jump = CrossPlatformInput$$anonymous$$anager.GetButtonDown("Jump");
}
a last part
if (m_CharacterController.isGrounded) {
doublejumppossible = true;
}
EDIT: Everything is in void Update except for the private bool and I now know that the if (doublejumppossible == true){ doublejumppossible = false; } is wrong
O$$anonymous$$ here's the script:
public class FirstPersonController : $$anonymous$$onoBehaviour {
private bool doublejumppossible;
private bool doublejump;
private bool prevjumped;
// Use this for initialization
private void Start()
{
prevjumped = false;
}
// Update is called once per frame
private void Update()
{
RotateView();
// the jump state needs to read here to make sure it is not missed
if (!m_Jump)
{
m_Jump = CrossPlatformInput$$anonymous$$anager.GetButtonDown("Jump");
}
if (doublejumppossible == true && prevjumped == true) {
doublejump = CrossPlatformInput$$anonymous$$anager.GetButtonDown("Jump");
}
if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
{
StartCoroutine(m_JumpBob.DoBobCycle());
PlayLandingSound();
m_$$anonymous$$oveDir.y = 0f;
m_Jumping = false;
}
if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
{
m_$$anonymous$$oveDir.y = 0f;
}
m_PreviouslyGrounded = m_CharacterController.isGrounded;
if (m_CharacterController.isGrounded) {
doublejumppossible = true;
prevjumped = false;
}
}
private void PlayLandingSound()
{
m_AudioSource.clip = m_LandSound;
m_AudioSource.Play();
m_NextStep = m_StepCycle + .5f;
}
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 desired$$anonymous$$ove = 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);
desired$$anonymous$$ove = Vector3.ProjectOnPlane(desired$$anonymous$$ove, hitInfo.normal).normalized;
m_$$anonymous$$oveDir.x = desired$$anonymous$$ove.x*speed;
m_$$anonymous$$oveDir.z = desired$$anonymous$$ove.z*speed;
if (doublejump == true) {
m_$$anonymous$$oveDir.y = m_JumpSpeed;
m_Jump = false;
m_Jumping = true;
doublejumppossible = false;
doublejump = false;
}
if (m_CharacterController.isGrounded)
{
m_$$anonymous$$oveDir.y = -m_StickToGroundForce;
if (m_Jump)
{
m_$$anonymous$$oveDir.y = m_JumpSpeed;
PlayJumpSound();
m_Jump = false;
m_Jumping = true;
prevjumped = true;
}
}
else
{
m_$$anonymous$$oveDir += Physics.gravity * m_Gravity$$anonymous$$ultiplier * Time.fixedDeltaTime;
}
m_CollisionFlags = m_CharacterController.$$anonymous$$ove(m_$$anonymous$$oveDir*Time.fixedDeltaTime);
ProgressStepCycle(speed);
UpdateCameraPosition(speed);
m_$$anonymous$$ouseLook.UpdateCursorLock();
}
Removed most of the script because character limit...