Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 arash28134 · Aug 17, 2021 at 08:51 PM · charactercontrollerjumpingcharacter movement

Character jumps immediately after it's grounded when jump is pressed mid-air

Hello. I have the following script for my character movement. the problem is, when the character is in mid-air and the jump key (space) is pressed, it jumps automatically after it lands on the ground. my code is:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class Movement : MonoBehaviour
 {
     [SerializeField] float moveSpeed = 6;
     [SerializeField] float jumpHeight = 2;
     [SerializeField] float gravity = 20;
     [Range(0, 10), SerializeField] float airControl = 5;
     Vector3 moveDirection = Vector3.zero;
     CharacterController controller;
     bool IsJumpPressed;
     void Start()
     {
         controller = GetComponent<CharacterController>();
     }
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             IsJumpPressed = true;
         }
     }
     void FixedUpdate()
     {
         var input = new Vector3(
         Input.GetAxis("Horizontal"),
         0,
         Input.GetAxis("Vertical")
         );
         input *= moveSpeed;
         input = transform.TransformDirection(input);
         if (controller.isGrounded)
         {
             moveDirection = input;
             if (IsJumpPressed)
             {
                 moveDirection.y = Mathf.Sqrt(2 * gravity * jumpHeight);
                 IsJumpPressed = false;
             }
             else
             {
                 moveDirection.y = 0;
             }
         }
         else
         {
             input.y = moveDirection.y;
             moveDirection = Vector3.Lerp(moveDirection, input,
             airControl * Time.deltaTime);
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
     }
 }


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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Aug 17, 2021 at 09:46 PM

      if (Input.GetKeyDown(KeyCode.Space) && controller.isGrounded)
      {
          IsJumpPressed = true;
      }


OR

set IsJumpPressed to false at the end of the FixedUpdate

Comment
Add comment · Show 1 · 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
avatar image arash28134 · Aug 17, 2021 at 10:02 PM 0
Share

Thanks, This fixed the issue (I tried: && controller.isGrounded) Have a nice day.

avatar image
0

Answer by bigcartert101 · Aug 17, 2021 at 09:08 PM

  if (Input.GetKeyDown(KeyCode.Space))
          {
              IsJumpPressed = true;
          }

This makes it so that IsJumpPressed stays true until a jump happens, and a jump happens if you are on the ground and IsJumpPressed=true. That is why you are jumping immediately. try

  if (controller.isGrounded)
          {
              moveDirection = input;
              if (Input.GetKeyDown(KeyCode.Space))
              {
                  moveDirection.y = Mathf.Sqrt(2 * gravity * jumpHeight);
              }
              else
              {
                  moveDirection.y = 0;
              }
          }


and not have the IsJumpPressed.

Comment
Add comment · Show 4 · 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
avatar image arash28134 · Aug 17, 2021 at 09:13 PM 0
Share

That Fixes the issue, but creates another problem. The reason i used 'IsJumpPressed' in the 'Update Method' was because if you use it in 'FixedUpdate' it will only detect it when you press the key at the start of a frame (i guess, sort of thing) and that makes it pretty hard to jump, you have to press space several times until one hits right.

avatar image bigcartert101 arash28134 · Aug 17, 2021 at 09:17 PM 0
Share

what if you did a late update to set IsJumpPressed to false?

avatar image arash28134 bigcartert101 · Aug 17, 2021 at 09:22 PM 1
Share

I did that, now it won't jump at all

Show more comments

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

185 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 avatar image avatar image avatar image 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 I modify this to allow jumping with space? 0 Answers

How to move a character in one direction only when jumping? 1 Answer

Character's jumping mechanism stuck into something invisible 1 Answer

how to jump in a 2D game 2 Answers

Error CS1061 - Error CS0103 - Error CS0165 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