- Home /
My player is lagging/stuttering when trying to move mid-air(after jumping)
Here is my code:
[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
public float _speed;
public float jumpForce;
private bool isGrounded;
public Vector3 jump;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
private void OnCollisionStay(Collision collision)
{
isGrounded = true;
}
private void OnCollisionExit(Collision collision)
{
isGrounded = false;
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
transform.Translate(new Vector3(horizontalInput, 0, verticalInput) * _speed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
rb.AddForce(jump * jumpForce, ForceMode.Impulse );
}
}
}
Note: The higher the jump is, the bigger the lag/stuttering gets I'm a total beginner and i've been trying to figure this out for the last 3 hours but I just can't find the problem. I thought it was because of the camera and tried to build the game but it lags anyways
Answer by CoreDLL · Mar 15, 2021 at 08:52 PM
Because you use the RigidBody's Forces together with Transform.Translate(). You make jumps only with Transform.Translate() which is a little hard, or you move Character in all directions using only the Rigidbody's forces;
Can you please explain how do I fix this because I'm a noob and to be honest I don't really know what to do
Instead of Translate use AddForce to make the movement be done only by Forces :
rb.AddForce(new Vector3(horizontalInput, 0, verticalInput) * _speed);
And make sure that you don't multiply values that you put in AddForce with Time.deltaTime because the Rigidbody is doing this automatically.
thanks but.. now it doesn't jump at all
Your answer
Follow this Question
Related Questions
How to make your character jump? 1 Answer
Character floats down after jumping 0 Answers
Dampening Movement while in the Air 0 Answers
How to prevent player from moving in air while jumping in place ?? 0 Answers