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 /
avatar image
0
Question by GodsStrength · Apr 11, 2020 at 11:17 AM · movement scriptjumping

How to prevent player from moving in air while jumping in place ??

Hi. I made a new project and I'm working on the movement of my first person character.

My main problem is that when I press space to jump while the player is not moving, after player gets in air, it still moves in air (which is not realistic)

How to solve that ??

Here is the code

public class PlayerMovement : MonoBehaviour { public CharacterController controller;

 public float normalSpeed = 8f;
 public float sprintSpeed = 16f;
 float currentspeed = 0;
 public float gravity = -40f;

 public Transform groundCheck;
 public float groundDistance = 0.4f;
 public LayerMask groundMask;
 public float JumpHeight = 3f;

 Vector3 velocity;
 bool isGrounded;

 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

     if(isGrounded && velocity.y < 0)
     {
         velocity.y = -2f;
     }

     float x = Input.GetAxis("Horizontal");
     float z = Input.GetAxis("Vertical");

     Vector3 move = transform.right * x + transform.forward * z;

     controller.Move(velocity * Time.deltaTime);
     velocity.y += gravity * Time.deltaTime;

     if (Input.GetKey(KeyCode.LeftShift) && isGrounded)
     {
         currentspeed = sprintSpeed;
         controller.Move(move * currentspeed * Time.deltaTime); // Sprint Movement Speed
         if (Input.GetButtonDown("Jump") && isGrounded)
         {
             velocity.y = Mathf.Sqrt(JumpHeight * -2 * gravity);
             
         }
     }
     else if (Input.GetKey(KeyCode.LeftShift) && isGrounded == false)
     {
         currentspeed = sprintSpeed;
         controller.Move(move * currentspeed * Time.deltaTime); // Sprint Movement Speed
     }
     else
     {
         currentspeed = normalSpeed;
         controller.Move(move * currentspeed * Time.deltaTime); // Normal Movement Speed
         if (Input.GetButtonDown("Jump") && isGrounded)
         {
             velocity.y = Mathf.Sqrt(JumpHeight * -2 * gravity);
         }
     }
 }

}

Comment
Add comment · Show 4
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 DCordoba · Apr 11, 2020 at 11:53 AM 0
Share

I think that happen because you allow the player to move in air while jumping, explicit from line 48 to 52

  else if (Input.GetKey(KeyCode.LeftShift) && isGrounded == false)
  {
      currentspeed = sprintSpeed;
      controller.$$anonymous$$ove(move * currentspeed * Time.deltaTime); // Sprint $$anonymous$$ovement Speed
  }

so I think you need to disable the jump while IsGrounded is false, so delete this part of the code

also add a exeption to normal speed on line 56

 if(isGrounded)
      controller.$$anonymous$$ove(move * currentspeed * Time.deltaTime); // Normal $$anonymous$$ovement Speed
avatar image GodsStrength · Apr 11, 2020 at 12:17 PM 0
Share

Well in real life, when you run and then jump, you actually move in both vertically and horizontally, so its normal to move in air while you are moving or sprinting.

I want to add a condition when you press Space first and jump in air and then you press WASD, you shouldnt be able to move in air, only moving in vertical direction

avatar image DCordoba GodsStrength · Apr 11, 2020 at 12:37 PM 0
Share

then set float x; and float z; as global vars and disable the input when you are not grounded, keeping the last value

  float x = 0f;
  float z = 0f;
  void Update()
  {
       //lines from 24 to 29 here
       if(isGrounded)
       {
            x = Input.GetAxis("Horizontal");
            z = Input.GetAxis("Vertical");
       }
       //lines after line 31...
avatar image GodsStrength · Apr 11, 2020 at 01:03 PM 1
Share

Thank you so much !!! That fixed my problem !!! <3

0 Replies

· Add your reply
  • Sort: 

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

128 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

Related Questions

This script doesn't make smooth jump why ? 2 Answers

Raycast doesn't detect the ground when the player is on a ledge. 1 Answer

How can I fix my velocity breaking when I rotate the character? 0 Answers

i am trying to make some movement for my character but i am having some issues when it comes to jumping 1 Answer

Dampening Movement while in the Air 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