• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by dogfart69 · Jan 27, 2019 at 12:07 AM · jumptimerjumpinggetbuttondown

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();
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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; }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

170 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how can make my player to jump like parabola 1 Answer

Mission Impossible question. make player move sideways more in air when jumping. 0 Answers

Why does my character stop jumping after he jumped twice? **i'm a begginer**,I'm trying to make my character to double jump. After i've jumped twice, it doesn't jump anymore at all. Why? 0 Answers

Jumping for longer 2d game 0 Answers

how to stop infinite jumping in air? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges