Using the same button for Jump and Levitate
I want to be able to use space bar to Jump, then again in the air to levitate. The Jump uses unity standard assets first person controller. The Levitate so far just uses:
if (Input.GetButton("Jump")) { m_MoveDir.y = 0; }
However, the GetButton is called before the player can release the space bar after using GetButtonDown to Jump. How do I create a delay before GetButton will register after GetButton Down?
More Code:
private void Update()
{
RotateView();
// this is to prevent "pre jumping"
m_Jump &= m_CharacterController.isGrounded;
// the jump state needs to read here to make sure it is not missed
if (!m_Jump)
{
m_Jump = Input.GetButtonDown("Jump");
}
if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
{
PlayLandingSound();
m_MoveDir.y = 0f;
m_Jumping = false;
}
if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
{
m_MoveDir.y = 0f;
}
m_PreviouslyGrounded = m_CharacterController.isGrounded;
}
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;
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;
}
//LEVITATE FUNTION
if (Input.GetButton("Jump"))
{
m_MoveDir.y = 0;
}
m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
ProgressStepCycle(speed);
m_MouseLook.UpdateCursorLock();
}
Answer by theTrueJellyfish · Jan 27, 2019 at 02:57 PM
try doing something like this. I originally used something similar for a 2d game so sorry if the code is a little off. @dogfart69
public Transform checkGroundPosition;
[SerializeField]
private float checkGroundRadius = 0.3f;
public LayerMask whatIsGround;
private bool jump = false;
private bool levitate = false;
grounded = "OverLapCircleWithThesePeramaters"(checkGroundPosition.position, checkGroundRadius, whatIsGround);
Levitate = 1;
//to jump the first if(grounded == true && input.GetButtonDown("Jump")) { Jump = true; Levitate = 1 }
if(grounded == false && Levitate == 1 && input.GetButtonDown("Jump)) { Levitate = true; Levitate-- }
In fixed update:
if(jump == true) { //code for jumping jump == false }
if(Levitate == true){ //code for levitation Levitate--; Levitate = false; }