- Home /
when i jump in the middle of running the player speed slows down mid air instead of keeping the constant pace I want for the move speed I set.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody rigCom;
[SerializeField] private Transform groundCheckTransform = null;
[SerializeField] private LayerMask playerMask;
private float moveSpeed;
private float moveHorizontal;
private bool jumpKeyWasPressed;
// Start is called before the first frame update
void Start()
{
rigCom = GetComponent<Rigidbody>();
moveSpeed = 3f;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
jumpKeyWasPressed = true;
}
moveHorizontal = Input.GetAxis("Horizontal");
}
//fixedupdate is called once every physic update
private void FixedUpdate()
{
rigCom.velocity = new Vector3(moveHorizontal, rigCom.velocity.y, 0);
if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f, playerMask).Length == 0)
{
return;
}
if (jumpKeyWasPressed)
{
rigCom.AddForce(Vector3.up * 6, ForceMode.VelocityChange);
jumpKeyWasPressed = false;
}
if (moveHorizontal > 0.1f || moveHorizontal < -0.1f)
{
rigCom.AddForce(new Vector3(moveHorizontal * moveSpeed, 0f, 0f), ForceMode.VelocityChange);
}
}
}
Comment
It could be ForceMode.VelocityChange causing this to happen.
Maybe try rigCom.velocity = Vector2.up * 6 instead to make your player jump.
I think it is the RigidBody drag value. Try setting it to 0 in the Inspector or do rigCom.drag = 0f;
Your answer
