Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

Problems with jumping in Doodle Jump like game 2 Answers

How i make a ball jump where i point with my mouse?[3D] 0 Answers

Jump logic issues 0 Answers

How to stop my character from infinite jumping? 2 Answers

Hello! I have infinite jump problem, please help me! 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges