- Home /
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;
}
}
}
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
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.
Thank you for pointing out the error to me, it worked and I am grateful for your help!
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; } }