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 /
avatar image
0
Question by Phizzy · Jun 10, 2021 at 04:28 AM · movementmultiplayerjumping

Jumping while moving

I'm making a multiplayer game and I have movement including jumping, the problem is that while still, I can jump just fine, but when I move it won't let me jump at all.

I'm using the newer input system, Here is my code:

 using Mirror;
 using System.Collections;
 using Mischla.kwg.game.Inputs;
 using UnityEngine;
 namespace Mischla.kwg.game
 {
     public class Player : NetworkBehaviour
     {
         [SerializeField] private float movementSpeed = 10f;
         [SerializeField] private CharacterController controller = null;
         [SerializeField] private float jumpHeight = 3f;
         [SerializeField] bool jump;
         [SerializeField] float gravity = -13.0f;
         bool isGrounded;
         float veloctiyY = 0.0f;
         private Vector2 previousInput;
 
         private Controls controls;
         private Controls Controls
         {
             get
             {
                 if (controls != null) {return controls; }
                 return controls = new Controls();
             }
         }
 
         public override void OnStartAuthority()
         {
             enabled = true;
 
             Controls.Player.Move.performed += ctx => SetMovement(ctx.ReadValue<Vector2>());
             Controls.Player.Move.canceled += ctx => ResetMovement();
             Controls.Player.Jump.performed += ctx => OnJumpPressed();
         }
 
         [ClientCallback]
         private void OnEnable() => Controls.Enable();
         [ClientCallback]
         private void OnDisable() => Controls.Disable();
         [ClientCallback]
         private void Update()
         {
             Move();
 
             if (jump == true) {
                 if(isGrounded == true){
                     veloctiyY = Mathf.Sqrt(jumpHeight * -2f * gravity);
                     StartCoroutine(Jumpy());
                 }
             } 
 
         }
         [Client]
         private void SetMovement(Vector3 movement) => previousInput = movement;
 
         [Client]
         private void ResetMovement() => previousInput = Vector2.zero;
 
         [Client]
         public void OnJumpPressed ()
         {
             jump = true;
         }
 
         [Client]
         private void Move()
         {
             Vector3 right = controller.transform.right;
             Vector3 forward = controller.transform.forward;
             right.y = 0f;
             forward.y = 0f;
 
             if(controller.isGrounded)
                 veloctiyY = 0.0f;
 
             veloctiyY += gravity * Time.deltaTime;
 
             isGrounded = controller.isGrounded;
 
             Vector3 movement = (right.normalized * previousInput.x + forward.normalized * previousInput.y) + Vector3.up * veloctiyY;
 
             controller.Move(movement * movementSpeed * Time.deltaTime);
         } 
 
         [Client]
         private IEnumerator Jumpy()
         {
             yield return new WaitForSeconds(0.01f);
             jump = false;
         }
     }
 }



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
0
Best Answer

Answer by DenisIsDenis · Jun 11, 2021 at 04:17 AM

As the CharacterController moves, it bounces unnoticed, which makes isGrounded equal to false. Therefore, when the character is on the ground, set velocitiyY to a negative number to pin him to the ground if isGrounded. Change to this condition:

 if(controller.isGrounded) // in the Move()
     veloctiyY = -0.25f;

EDITED: @Phizzy I tested your script and found where the error is. You defined the click on the jump button after the actual movement of the character using the controller.Move () function. Therefore, the jump did not work correctly. I changed your script as needed (that is, put the condition from the Update() function into your Move () function):

  using Mirror;
  using System.Collections;
  using Mischla.kwg.game.Inputs;
  using UnityEngine;
  namespace Mischla.kwg.game
  {
      public class Player : NetworkBehaviour
      {
          [SerializeField] private float movementSpeed = 10f;
          [SerializeField] private CharacterController controller = null;
          [SerializeField] private float jumpHeight = 3f;
          [SerializeField] bool jump;
          [SerializeField] float gravity = -13.0f;
          bool isGrounded;
          float veloctiyY = 0.0f;
          private Vector2 previousInput;
  
          private Controls controls;
          private Controls Controls
          {
              get
              {
                  if (controls != null) {return controls; }
                  return controls = new Controls();
              }
          }
  
          public override void OnStartAuthority()
          {
              enabled = true;
  
              Controls.Player.Move.performed += ctx => SetMovement(ctx.ReadValue<Vector2>());
              Controls.Player.Move.canceled += ctx => ResetMovement();
              Controls.Player.Jump.performed += ctx => OnJumpPressed();
          }
  
          [ClientCallback]
          private void OnEnable() => Controls.Enable();
          [ClientCallback]
          private void OnDisable() => Controls.Disable();
          [ClientCallback]
          private void Update()
          {
              Move();
          }
          [Client]
          private void SetMovement(Vector3 movement) => previousInput = movement;
  
          [Client]
          private void ResetMovement() => previousInput = Vector2.zero;
  
          [Client]
          public void OnJumpPressed ()
          {
              jump = true;
          }
  
          [Client]
          private void Move()
          {
              Vector3 right = controller.transform.right;
              Vector3 forward = controller.transform.forward;
              right.y = 0f;
              forward.y = 0f;
  
              if(controller.isGrounded)
                  veloctiyY = -0.1f; // I recommend using this -0.1f

              // "if" from Update() //
              if (jump == true) {
                  if(isGrounded == true){
                      veloctiyY = Mathf.Sqrt(jumpHeight * -2f * gravity);
                      StartCoroutine(Jumpy());
                  }
              } 
              //---------------------------//

              veloctiyY += gravity * Time.deltaTime;
  
              isGrounded = controller.isGrounded;
  
              Vector3 movement = (right.normalized * previousInput.x + forward.normalized * previousInput.y) + Vector3.up * veloctiyY;
  
              controller.Move(movement * movementSpeed * Time.deltaTime);
          } 
  
          [Client]
          private IEnumerator Jumpy()
          {
              yield return new WaitForSeconds(0.01f);
              jump = false;
          }
      }
  }

Also I recommend using this -0.1f in if(controller.isGrounded) condition

Comment
Add comment · Show 2 · 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 Phizzy · Jun 11, 2021 at 05:16 AM 0
Share

I tried your suggestion and had to tweak it a little bit but it did not work, if I set the number any higher than -0.08 then it won't let me jump, but lower or equal to -0.08 gives me my original problem, I'm sincerely grateful for the help, but it did not work for me.

avatar image Phizzy · Jun 11, 2021 at 09:48 PM 0
Share

Thank you for pointing out the error to me, it worked and I am grateful for your help!

avatar image
0

Answer by mjood-10 · Jun 10, 2021 at 08:06 AM

I guess your problem is in this line. veloctiyY = Mathf.Sqrt(jumpHeight -2f gravity);

you are assigning a value to velocityY after you get out of Move() function. and the next time Update is called VelocityY gets a new value in Move()

I think if you assign the value false to is Grounded it might solve your problem.

try putting

if (jump == true) { if(isGrounded == true){ veloctiyY = Mathf.Sqrt(jumpHeight -2f gravity); StartCoroutine(Jumpy()); isGrounded = false; } }

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 Phizzy · Jun 10, 2021 at 09:34 PM 0
Share

It didn't work, but I appreciate the help!

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

249 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 add jumping to my multiplayer photon script? 0 Answers

Multiplayer problem - 2nd player erratic movment? 0 Answers

Rocket Jumping Woes 1 Answer

Simple Rigidbody2D movement and jump 0 Answers

Networked player movement very choppy, rotation works fine - UNET 1 Answer


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